views:

72

answers:

4

Hi,

I created a small chat program, that works flawlessly when client & server are run on the same computer (and probably network, too). However, as soon as I try to connect to another computer over the internet, the socket connection simply times out. Is this because of firewalls / routers, etc?

And how can I connect a ServerSocket & Socket over the internet?

A: 

First, test to see if it really works on a LAN; it sounds like you're just assuming it does.

If it works on your local network, perhaps it's failing because the server lacks a public IP, and is running behind a NAT'ing router.

Paul A Jungwirth
How would I know my server lacks a public IP? Both my laptop and my iPod show the same IP on whatismyip.com, does that mean my server (laptop) doesn't have a public IP?
Jake
+1  A: 

If your server is behind a NAT router box (and most home computers are, especially if you use WiFi), then it won't be reachable from the outside unless you set up your router to port forward to that server.

What's the IP of your server computer? If it's 192.168.x.x or 10.x.x.x, then it's a non-routable address and can't be reached from outside.

Paul Tomblin
But then how can servers from all over the world (MSN, ICQ, mail, etc) contact my computer?
Jake
Well, the local network IP (smth. like 192.168.xxx) is different from the internet IP (smth. else, checked with whatismyip.com), but the public IPs are identical on my iPod and Laptop....
Jake
It's your computer that connects to them not the otherway around. You can connect to any node with public IP address. If the server is behind a NAT, the NAT rounter can be configured to redirect connections and such, but that's a superuser question
notnoop
@Jake, as I said in my answer, you have to set up port forwarding on your router.
Paul Tomblin
+3  A: 

However, as soon as I try to connect to another computer over the internet, the socket connection simply times out. Is this because of firewalls / routers, etc?

Yes, most likely. You're running into the NAT problem: essentially, the same externally visible IP address maps to many internally visible endpoints, and external endpoint doesn't know which internal endpoint to give your socket request to.

The easiest way around this is to have both your clients connect to a third party which both of them can see, and then have the third party mediate the communication. This is how most instant-messaging protocols work, for example.

If you have no way to control a third-party entity like that, an alternative to directly connect two clients is to have both clients open up an agreed-upon port, and then map communications on that port to their own internal endpoint. This provides the missing link that the externally visible endpoint (e.g. your home router) needs to deliver the communication to its intended destination.

John Feminella
Yeah, but I fear I don't have such a third party...
Jake
@Jake: I suspected that might be the case -- see my last paragraph. Basically, have your chat program use some arbitrary port (if this is IPv4, pick a number above 1024, though, because those are reserved), and require that both clients have this port open on their routers.
John Feminella
+1  A: 

Assuming with running on the same computer you mean that you tell the client the server is at 127.0.0.1 / localhost, it shouldn't be a problem in your code but because of firewalls or routers. If your server is behind a router performing masquerading (i.e., the server doesn't have a public but private IP address like 192.168.x.y for instance), you have to configure the router to pass a connection from the internet to the computer running the server.

Another reason why it doesn't work might be the way you bind your server to the interface. If you specify 127.0.0.1 there, the server will only listen for requests coming from the same system. If you don't specify an address, it will listen on all interfaces.

Edit Your comment indicates that you indeed have the NAT problem like others said. Configuring your router accordingly is probably the easiest solution.

ahans