!!! localhost all OK, but in reality packets never arrive !!! HELP !!!

I have a serious problem with the application i work. I implement a multiplayer mode for a game using NIO. Wher i run the application all look fine but when i install the server in another computer the packets never arrive through network.

How can i fix this problem???

Here is the code:

SERVER
[b]
import java.net.;
import java.io.
;
import java.nio.;
import java.nio.channels.
;

public class NIOServer {

public final static int DEFAULT_PORT = 3120;
public final static int MAX_PACKET_SIZE = 65507;

public static void main(String[] args) {

int port = DEFAULT_PORT;
    
try {
  DatagramChannel channel = DatagramChannel.open();
  DatagramSocket socket = channel.socket();
  SocketAddress address = new InetSocketAddress(port);
  socket.bind(address);
  ByteBuffer buffer = ByteBuffer.allocateDirect(MAX_PACKET_SIZE);
  while (true) {
    SocketAddress client = channel.receive(buffer);
    //data s=buffer.getBytes();
    //System.out.print(buffer);
    buffer.flip();
    channel.send(buffer, client);
    buffer.clear();
  } // end while
}  // end try
catch (IOException ex) {
  System.err.println(ex);
}  // end catch

} // end main

}[/b]

CLIENT
[b]
import java.net.;
import java.io.
;
import java.nio.;
import java.nio.channels.
;
import java.util.*;

public class NIOClient{
public final static int DEFAULT_PORT = 3120;
private final static int LIMIT = 1000;
int port = DEFAULT_PORT;

SocketAddress remote;
DatagramChannel channel;
Selector selector;
ByteBuffer buffer;
public NIOClient(){
	try{
		remote = new InetSocketAddress("localhost", port);
		channel = DatagramChannel.open();
		channel.configureBlocking(false);
  		channel.connect(remote);
  		selector = Selector.open();
  		channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  		buffer = ByteBuffer.allocate(4);
	}
	catch (IOException ex) {
  		System.err.println(ex);
	}
	
}

public int channel(int k){
	int r=0;
	try {
       
  		int n = 0;
  		int numbersRead = 0;
  			// wait one minute for a connection
    		selector.select(6000);
    		Set readyKeys = selector.selectedKeys();
    		Iterator iterator = readyKeys.iterator();
      			if (iterator.hasNext()) {
        			SelectionKey key = (SelectionKey) iterator.next();
        			iterator.remove();
        			if (key.isReadable()) {
          				buffer.clear();
          				channel.read(buffer);
          				buffer.flip();
          				int echo = buffer.getInt();
          				System.out.println("Read: " + echo);
          				numbersRead++;
          				r=echo;
        			} 
        			if (key.isWritable()) {
          				buffer.clear();
          				buffer.putInt(k);
          				buffer.flip();
          				channel.write(buffer);
          				System.out.println("Wrote: " + k);
          				n++;
         				if (n == LIMIT) {
            				// All packets have been written; switch to read-only mode
            				key.interestOps(SelectionKey.OP_READ);
          				} 
        			}  
      			} 
    		  
	}  // end try
	catch (IOException ex) {
  		System.err.println(ex);
	}
	return r;
}

}[/b]


remote = new InetSocketAddress("localhost", port);

You are still trying to connect to the locla computer, not the remote one. You need to replace localhost with the ip address of the computer running the server.

i know my friend…of course…this is when i have the problem !!!

Then show the code you are using, not the code you were using. We are not mind readers and know that you made the correct changes. People have made simple mistakes before.

Friend, you bring me problems…If you do not understand let the others understand. I do not have something to change, if you want to see the problem just put an IP address. p.x. 167.45.0.7. Replace the word “localhost”. It is simple and you confuse the others in what i ask!

Please, check the problem. I have turned off the firewalls, i have checked the packet size and till the packets never arrive to the server when the systems runs on internet. I repeat that localhost and in a lan network all look fine.

Yeah, ever heard of “never bite the hand that feeds you” ?

You seem to have quite an attitude for somebody who has a trival error in its sourcecode which somebody else actually takes the times to read and spot the error.

Then you say it doesn’t work when dealing with more than 1 computer (first post) while in your last post you say it “I repeat that localhost and in a lan network all look fine.” - you never said LAN was working.

So to get back to the original problem, and hoping you also don’t burn my advise to the ground: you need to setup proper port-forwarding or setup a NAT server in your router for WAN->LAN bound traffic.

Last: seriously, drop that attitude.

Dear Riven,

The information i added were not available to me at the time of my first post. Athough you really misunderstood me, i apologise for my “attitude”, i just wanted to be specific and our discussion not to take irrelevant extensions. Sory again Riven and CaptainJester.

As far as the information you gave me is concerned, i have already done port forwarding and nothing changed. Both from the sides of client and server. Have you checked the code Riven?

As you never seem to call

public int channel(int k)

anywhere, it doesn’t make any sense it even works on localhost/LAN.

And if this is yet another-way-too-easy-to-spot-bug-that-has-long-been-fixed… you might want to give more recent code.

Last but not least, you say you wait for 6 minutes in your code, but actually wait 6000ms = 6 seconds. By the time you launch your client the 6 seconds might already have passed.

Riven it is just a class. It does not have a main. I have another class that calls it. Anyway…

As for the waiting time i fixed it because one minute was a lot of time and i forgot to erase the comment.

The problem is not there i think… I really do not know…

Thanks for your response

If we don’t get to see all relevant code, how could we help you?

Goodluck with it…