Socket.connect () to 0.0.0.0: Windows vs Mac

Provide the following code:

String hostName = "0.0.0.0"; int port = 10002; int timeout = 5000; Socket socket = new Socket(); socket.connect(new InetSocketAddress(hostName, port), timeout); 

It works fine on a Mac and makes a connection (even if it’s not working on port 10002), and on Windows I get the following exception:

 java.net.SocketException: Permission denied: connect 

What is the difference here and what will be the alternative to Windows? It is used in unit tests.

Hello

Jonas

+4
source share
1 answer

Just in case, someone else came across this question, I answer it.

Unfortunately, connecting to any address is not allowed on Windows.

The Winsock connection function will return the WSAEADDRNOTAVAIL error code [The remote address is not a valid address (for example, INADDR_ANY or in6addr_any)], as indicated in the Windows API Documentation :

If the structure address member specified by the name parameter is filled with zeros, the connection will return a WSAEADDRNOTAVAIL error.

Therefore, without using any localhost address, I think that what you are trying to do will not be possible on Windows (although I am wondering if the Unix behavior is buggy or intentional.).

I would suggest setting up additional loopback interfaces, as suggested by Mark Reed in comment .

+3
source

All Articles