Communicating between two different computers with

2019-08-11 22:52发布

问题:

I've been studying how to use sockets to make two java programs communicate.

Now, on each example I've gone through, they've always placed the "server side program" on the same computer on which the "client side program" was, and of course the IP set was either 127.0.0.1 , or simply "localhost".

Now, my question is: how do I get to communicate the two programs if one is on a computer, and one on another?

I've tried to set the IP as the one my computer has when connected to internet (the one I found simply by googling "what's my IP"), but that way it doesn't seems to work. Can anyone explain me better how I can make it work?

回答1:

The computers will need to be on the same network or their IP address will need to be accessible to the internet. If you are in windows, you can find the local IP address by opening a cmd window (press windows key + R then type cmd) and running ipconfig. On linux (maybe it works on Mac as well) you can run ifconfig in a terminal. If your computers are on the same local area network you should be able to use the IP addresses indicated by those commands.

If they are not on the same local area network, a router in between the computers may have a firewall policy which is blocking the connection.