When multiple computers have identical IP addresses, how can you connect to one of them programmatically?
This question is a bit hard to phrase and I'm not sure if I understand the underlying issue, but here goes: I have a java chat program. I run it using 2 jar files - a server and a client. You run the 开发者_如何学Cserver in the background on a computer, and then when you run the client you just have to enter the ip address of the computer that's running the server and hit connect. I've used it several times before, and it has always worked. My problem now is that although I can still get it to work within our home network by using the local ip address (the one that begins with 192), I can't get it to work using the public / global ip address. I've noticed that all our home computers, which are all connected to the same modem (router?), have the same global ip address (I checked whatismyipaddress.com). Could this be the problem? If all our computers have the same IP address, how does the program know which one to go to for the server program? Any ideas on how I can get this to work?
Thanks!
This really belongs on serverfault. But the short answer is that you need to set up port forwarding on your router. This tells the router, for incoming requests on a given port, the internal IP address of the machine that those requests should be directed to. Check out http://www.portforward.com for detailed instructions.
What your network is doing is called "network address translation" or NAT. When a machine on the local network (client) wants to contact a machine outside the network (a server, such as an internet host with a public IP address) the router keeps track of where the packets should be sent and delivers them back to the appropriate client, even though packets destined for different clients on the local network will all come to the same IP.
This works fine if the client establishes a connection to the server. The server cannot initiate the connection back to the client nor can it arbitrarily send UDP packets unless, as danben mentioned, static port forwarding is set up.
There is UPnP which lets clients basically set up their own temporary port forwarding rules but I don't know much about the specifics.
What you should be able to do though is have a client establish a TCP/IP connection to the server and then all traffic on that connection, both upstream and downstream, should be handled fine by the router. If your server does not have a public IP then you will need to set up static port forwarding at the router such that all traffic coming in on a certain port is routed to a private IP inside the network. This likely means you'll need to assign a static private IP to the server as well.
Just because all the computers on the network have the same external IP address (when seen from outside your network) does not mean that they all have the same IP address inside your network.
More likely than not, your router is assigning internal/private IP addresses to each computer on the network, probably starting with 192.168 or 10.something. This is the IP address that computers inside your network use to communicate with each other.
精彩评论