7

Imagine 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);

On the Mac it works fine and executes the connect (even with nothing running on port 10002) and on Windows I get the following exception:

java.net.SocketException: Permission denied: connect

What's the difference here and what would be the alternative on Windows? This is used in unit tests.

Chenmunka
  • 685
  • 4
  • 21
  • 25
Jonas
  • 854
  • 13
  • 33
  • I didn't know it was possible to connect to the any address. Why don't you use the loopback address? – CodeCaster Aug 16 '12 at 07:34
  • The problem is that there are two nodes it's trying to connect to and they need to be on different hosts and it's already using the loopback address for the other one. So, the designer of the test used that and it seems fine on the Mac and Linux but not on Windows. I cannot use a random one because then it will timeout. – Jonas Aug 16 '12 at 07:42
  • 1
    @Jonas Who do you want 0.0.0.0 to connect to ? – nos Aug 16 '12 at 08:15
  • 1
    I just want the above lines to pass in Windows without an exception... – Jonas Aug 16 '12 at 08:36
  • 2
    You can't get them to pass without an exception. You cannot connect to that address. The operation you are attempting is meaningless and therefore illegal. You have to connect to a real IP address. – user207421 Aug 16 '12 at 10:23
  • 1
    Alright ... have to find a workaround but the weird thing is that on Linux there's no exception. – Jonas Aug 17 '12 at 02:04
  • I would either use different ports instead of different IP's, or bring up extra loopback interfaces with different 127.x IP addresses. I know that `connect()` to 0.0.0.0 works on Linux and OS X as a way to connect to localhost, but I don't know why. AFAIK it's not standard behavior. – Mark Reed Aug 10 '13 at 13:49
  • What are you expecting to happen? You are trying to establish a connection to a source host. How does that even make sense? http://tools.ietf.org/html/rfc5735#section-3 – Mikkel Løkke Jan 30 '15 at 06:15

1 Answers1

7

Just in case somebody else stumbles upon this question, I am answering it.

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

The Winsock function connect will return the error code WSAEADDRNOTAVAIL [The remote address is not a valid address (such as INADDR_ANY or in6addr_any)], as stated at the Windows API Documentation:

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

So without using any localhost address, I think what you are trying to do will not be possible on Windows (Though I wonder if the Unix behavior is a bug or intentional.).

I would suggest setting up more loopback interfaces, as Mark Reed suggested in his comment.

Community
  • 1
  • 1
ariganis
  • 300
  • 2
  • 7
  • 2
    Neither. The difference is that winsock will not let you intentionally create illegal packets. The BSD socket library does not place such restrictions upon developers, because they are expected to know what they are doing. However if you are successfully able to connect to a destination address of 0.0.0.0. you are breaking the Internet. – Mikkel Løkke Jan 30 '15 at 06:27