Bind the wildcard address, then ask whether this host is behind NAT
Bind the wildcard address, then ask whether this host is behind NAT
Answer
InetAddress any = InetAddress.getByName("0.0.0.0"); try (ServerSocket server = new ServerSocket(8000, 50, any)) { System.out.println("listening on " + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort()); InetAddress me = InetAddress.getLocalHost(); System.out.println(me.getHostAddress() + " private? " + me.isSiteLocalAddress()); }
The three-argument constructor takes the bind address explicitly, and `0.0.0.0` means every interface — the wildcard from the address lesson, doing its one job. The last line is the diagnosis: `isSiteLocalAddress()` is true for `192.168.1.5`, which says this host holds a private address and therefore that nothing outside can reach it without a forwarded port.
Harold 4e ch1 §The Internet; JDK javadoc java.net.ServerSocket