Client can connect to server on localhost, but not global IP?

Alright, so I’ve got my port forwarded, and I’ve tried it with all firewalls off, and in DMZ mode (All ports open), so It’s not a network problem. I’ve scanned my port with the server running, and the scanner picks up the port, and the server (Thinking it’s a player) prints out that a new player has connected.
The client can connect to the server on localhost, and 127.0.0.1, but not on any global IP. I’ve tried this on multiple networks (Giving friends the server, and client) but to no avail.

So I’m going to post some connection code up, and maybe one of you can tell me whats wrong? Also I’ll post up the client/server pack, in hopes that one of you can get it working, somehow.

Anyway, this really was my last place to ask for help, but I’ve tried this for 2 days now, and asked on SO and the game dev community I frequent.

Also, no, I’m only getting a timeout exception on the client after a little while, no other exceptions.


try {
            String ip = JOptionPane.showInputDialog("Please input a IP address");
            String port = JOptionPane.showInputDialog("Please input a open port number");
            mySocket = new Socket(ip, Integer.valueOf(port).intValue());
            fromServer = mySocket.getInputStream();
            toServer = mySocket.getOutputStream();
        } catch (Exception e) { e.printStackTrace(); }

Edit: forgot the link: https://dl.dropbox.com/u/28109593/cybatar-test.zip

What’s your listener code look like?

for the server? Well, I already know it’s not a server problem, but no harm in showing it off, I guess.

try { 
            final ServerSocket serverSocket = new ServerSocket(Integer.valueOf(JOptionPane.showInputDialog("Please input a open port number")).intValue());


            while(!stopServer){
                if(waitForPlayers){
                    Socket clientSocket = null;
                    try {
                        clientSocket = serverSocket.accept();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    playerID++;
                    playersConnected.add(new Player(playerID));
                    new PlayerThread(playerID, this, mainText, playerID, clientSocket).start();
                    mainText.append("Player " + playerID + " connected!");
                }

                else{
                    for(int i = 0; i < playersConnected.size(); i++){
                        playersConnected.get(i).update();
                    }
                }

                Thread.yield();
            }
        } catch (Exception e) { e.printStackTrace(); }

And here is the send/receive code in PlayerThread:

me = parent.playersConnected.get(myPlayer);
            try {
                if(mySocket.isConnected()){
                    int psize = (packetSize * (byte) parent.playersConnected.size()) + 7;
                    ByteBuffer bb = BufferUtils.createByteBuffer(psize);
                    //mainText.append("playersConnected: " + parent.playersConnected.size() + "\n");
                    //header start
                    bb.put((byte) 50);                  
                    bb.put((byte) 100);
                    bb.put((byte) 60);
                    bb.put((byte) 90);
                    //header end

                    bb.put((byte) parent.playersConnected.size());
                    bb.put(flag);
                    bb.put(me.id);

                    for(int i = 0; i < parent.playersConnected.size(); i++){
                        bb.put(parent.playersConnected.get(i).id);
                        bb.put(parent.playersConnected.get(i).state);
                        bb.putFloat(parent.playersConnected.get(i).pos.x);
                        bb.putFloat(parent.playersConnected.get(i).pos.y);
                    }
                    bb.flip();

                    byte[] bytesToSend  = new byte[psize];
                    bb.get(bytesToSend, 0, bytesToSend.length);

                    toPlayer = mySocket.getOutputStream();
                    toPlayer.write(bytesToSend);
                    toPlayer.flush();

                    byte[] recievedBytes = new byte[2];
                    fromPlayer = mySocket.getInputStream();

                    fromPlayer.read(recievedBytes);

                    if(recievedBytes != null){
                        mainText.append("playerID: " + recievedBytes[0] + " state:" + recievedBytes[1]  + "\n");
                        if(recievedBytes[0] == me.id) me.state = recievedBytes[1];
                    }
                    me.update();

                }

                Thread.sleep(60);
            } catch (Exception e) {
                die = true;
                e.printStackTrace(); 
            }

I was interested in how you were binding the server socket. The one-arg constructor for ServerSocket should definitely be binding to 0.0.0.0, so that’s not the problem. Just for grins, what does System.out.println(serverSocket.getLocalSocketAddress()) show?

Are you able to connect to the server on its non-localhost address on that machine itself? If it’s NAT’ed, i mean the internal one like 192.168.x.x or 10.x.x.x

Good question about the local IP thing. Yes, it does connect to my 10.x.x.x IP (10.0.0.4)
And the system.out.println gave me “0.0.0.0/0.0.0.0:44444” <— that.

Sounds like the problem is somewhere on the network, perhaps with your port forwarding. Worst case is, your ISP is blocking it. If you want to PM me the public IP I can try hitting it from a couple of my addresses.

No need, he can check if the port is open with this utility website.

Good thinking – I’m so used to popping over to my AWS box for an outside connection that I forget about the webby web :slight_smile:

Success: I can see your service on 67.160.14.227 on port (44444)
Your ISP is not blocking port 44444
from ra4king’s suggested site.

Ok so your router is port forwarding correctly to your computer, and your computer’s firewall has the port open so far.

Are you using that IP address to connect?

I’ve already tried to connect with both the PC and router firewall turned off, and in DMZ mode (All ports open). All failed.

PMed you.

If by “that” you mean the same one I’m getting off the site, then yes.

I don’t see what else could be done here. You use ServerSocket with the int parameter, giving it 44444. Your router port-forwards 44444 to your internal IP address. Your firewall has port 44444 open. Connecting to localhost on port 44444 works. Connecting to your public IP address on port 44444 fails.

Have you tried connecting to your private IP address?

Yes, connecting to local host, 127.0.0.1 or 10.0.0.4 (my lan address) works perfectly.

localhost doesn’t prove anything, your LAN address proves your firewall has the port open. So I’m blaming either your router or you.

Tried:

InetAddress addr = InetAddress.getByName(ip);
			SocketAddress remote = new InetSocketAddress(addr, 44444); 
			SocketChannel mySocketChannel = SocketChannel.open();
			mySocketChannel.connect(remote);
			
			System.out.println("doesn't get here");
			mySocket = mySocketChannel.socket();

As you can see it stalls on connect, and doesn’t connect to anything.

This isn’t a problem with java code. I cannot telnet to that port. Your network is the problem, full stop.

Okay, so if you can’t telnet, but port scanning sites can connect to it (and the server) what does that mean? Also, friends who have ran the client and server themselves have the same problem. I’m sure not all of them have problem networks.

So any diagnoses?

Double check the router and your computer’s firewall.