Unable to connect to socket on different networks

I am having problems connecting my online application to others through another network. I can give them hostAddress to connect when we are on the same network, but when we do it over the Internet, the generated host address does not allow the connection, nor does it use the ip address obtained from internet sites such as whatismyip.com

My biggest problem is not debugging this code, because it works over the network, but the server does not see attempts when we try to switch to different networks. In addition, the test port I am using is 2222.

InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();
System.out.println("Hostname: " + hostname);
System.out.println("IP: " + addr.getHostAddress());

I show the host on the server when it starts

if (isClient) {
System.out.println("Client Starting..");
clientSocket = new Socket(host, port_number); 
} else {
System.out.println("Server Starting..");
echoServer = new ServerSocket(port_number);
clientSocket = echoServer.accept();
System.out.println("Warning, Incoming Game..");
}
+3
source share
3 answers

, , , , NAT'ed connection, IP-, , IP- , . , , .

+7

, .

, ( ).

, 2222 ( 5555 , LAN ( ) 2222).

: ---- > ---- > [ ] internet_IP → [ ] lan_IP → lan_IP2

internet_IP -; : http://www.whatismyip.com/ lan_IP: . : 192.168.0.1 lan_IP2: (DHCP). , ipconfig (Windows) ifconfig (Linux).

2222 lan_IP2 2222. http-: http://192.168.0.1

+1

Some additional information may be helpful. Can you ping a car wherever you are? Are you trying to get through the firewall? You say that they work on a local host or local network - these are slightly different. Do you mean a network using local space (i.e. 10 ... * or 192.168 .. or the like)? You say you are using test ip 2222 - this is not an ip address. Is this an address domain? Or is it a port number?

0
source

Source: https://habr.com/ru/post/1740053/


All Articles