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?
解决方案
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.
本文探讨了如何使两个Java程序在不同计算机上通过Socket进行通信。通常示例中使用127.0.0.1或localhost进行本地通信。要实现跨网络通信,两台计算机需要在同一局域网内或彼此的IP地址需可互联网访问。通过Windows的cmd或Linux终端获取本地IP地址。如果不在同一局域网,可能需要考虑路由器的防火墙政策。
2301

被折叠的 条评论
为什么被折叠?



