Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Sockets time-out over the Internet

Tags:

java

sockets

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?

like image 727
Jake Avatar asked Jan 16 '10 16:01

Jake


1 Answers

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.

like image 164
John Feminella Avatar answered Nov 14 '22 20:11

John Feminella