Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Client can connect to server on localhost, but not global IP?  (Read 8315 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2012-07-12 19:55:27 »

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.
1  
2  
3  
4  
5  
6  
7  
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
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-07-12 20:03:21 »

What's your listener code look like?
Offline CyanPrime
« Reply #2 - Posted 2012-07-12 20:07:08 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
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();
            }




Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-07-12 20:36:01 »

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


Offline CyanPrime
« Reply #4 - Posted 2012-07-12 20:43:05 »

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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-07-12 20:57:51 »

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.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-07-12 21:01:53 »

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

Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-07-12 21:03:20 »

Good thinking -- I'm so used to popping over to my AWS box for an outside connection that I forget about the webby web Smiley
Offline CyanPrime
« Reply #8 - Posted 2012-07-12 21:03:51 »

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.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-07-12 21:06:21 »

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?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #10 - Posted 2012-07-12 21:07:11 »

I've already tried to connect with both the PC and router firewall turned off, and in DMZ mode (All ports open). All failed.
Offline CyanPrime
« Reply #11 - Posted 2012-07-12 21:07:37 »

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.

PMed you.
Offline CyanPrime
« Reply #12 - Posted 2012-07-12 21:08:55 »

Are you using that IP address to connect?
If by "that" you mean the same one I'm getting off the site, then yes.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-07-12 21:12:52 »

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?

Offline CyanPrime
« Reply #14 - Posted 2012-07-12 21:15:04 »

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.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2012-07-12 21:20:52 »

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

Offline CyanPrime
« Reply #16 - Posted 2012-07-13 01:58:58 »

Tried:

1  
2  
3  
4  
5  
6  
7  
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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #17 - Posted 2012-07-13 02:55:21 »

This isn't a problem with java code.  I cannot telnet to that port.  Your network is the problem, full stop.
Offline CyanPrime
« Reply #18 - Posted 2012-07-13 03:36:53 »

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?
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #19 - Posted 2012-07-13 03:54:31 »

Double check the router and your computer's firewall.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2012-07-13 03:59:51 »

Portforwarding on a router is for some reason a very brittle technology. I've had about 8 routers, and they all had nice web-interfaces that allowed you to setup portforwarding. Two of those eight actually managed to forward traffic...

You're better off renting a cheap VPS with little to no RAM, and using it solely to setup a reverse (SSH) tunnel: your desktop will connect to the VPS using TCP, and the outbound connection is used to pump traffic from the outside world to your desktop.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline CyanPrime
« Reply #21 - Posted 2012-07-13 04:04:16 »

Strange, when I disable port forwarding I get a connection refused with the client right away. Why doesn't it do that with port forwarding on? It's not connecting to the server  Huh.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #22 - Posted 2012-07-13 04:05:37 »

Strange, when I disable port forwarding I get a connection refused with the client right away. Why doesn't it do that with port forwarding on? It's not connecting to the server  Huh.
probably a firewall in between, or you picked the wrong local IP, or made any other mistake in the config. it's hard to diagnose without information. the serversocket might be listening on 127.0.0.1 instead of your LAN IP.

what about specifying the local IP: new ServerSocket(InetAddress.getByAddress("10.0.0.x"), 50, 44444)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline HeroesGraveDev

JGO Kernel


Medals: 300
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #23 - Posted 2012-07-13 04:14:02 »

I've had experiences like this with setting up a Minecraft server and I heard that you can't connect to your global IP from your own router. Because technically, the router is sending the data to itself

What I think you need to do is get a friend to try and connect and see if it works for the reason above

Someone correct me if I'm wrong.

Offline CyanPrime
« Reply #24 - Posted 2012-07-13 04:15:58 »

probably a firewall in between, or you picked the wrong local IP, or made any other mistake in the config. it's hard to diagnose without information. the serversocket might be listening on 127.0.0.1 instead of your LAN IP.

what about specifying the local IP: new ServerSocket(InetAddress.getByAddress("10.0.0.x"), 50, 44444)

Alright, I tried the normal code, then
1  
new ServerSocket(InetAddress.getByAddress("10.0.0.x"), 50, 44444)
. Both after turning off windows defender, all firewalls (router, and PC) and putting the router into all ports open mode.

Same results.
With port forwarding enabled I got a timeout after some time, and without port forwarding I got connection refused.

Thank you though, that was a good idea.  Cheesy
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #25 - Posted 2012-07-13 04:16:09 »

@HeroesGraveDev sproingie already make an attempt from another machine

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline jonjava
« Reply #26 - Posted 2012-07-13 08:35:29 »

Maybe your ip address is wrong. Try connecting to this ip http://www.whatismyip.com/.

Offline HeroesGraveDev

JGO Kernel


Medals: 300
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #27 - Posted 2012-07-13 10:56:20 »

@HeroesGraveDev sproingie already make an attempt from another machine

Didn't see that.

Just checking it wasn't a facepalm-level failure

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #28 - Posted 2012-07-13 17:09:09 »

CyanPrime, did you literally use 'x' in your code for that LAN IP address? You are supposed to replace it with the actual value. I'm just checking for all possibilities here Grin

Offline CyanPrime
« Reply #29 - Posted 2012-07-13 17:29:39 »

CyanPrime, did you literally use 'x' in your code for that LAN IP address? You are supposed to replace it with the actual value. I'm just checking for all possibilities here Grin
Had I done that I would have gotten a error, right?  Wink
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (61 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!