11

I've found empirically that

Endpoint endpoint1 = Endpoint.create(new Ping());
endpoint1.publish("http://0.0.0.0:8080/ws/ping");

binds to all network interfaces on the current computer (instead of just localhost - 127.0.0.1 or the hostname), but I have not been able to locate the documentation which says that this is guaranteed.

Question: Where is it defined that binding to 0.0.0.0 in Java will always bind to all network interfaces?

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347

3 Answers3

16

Using 0.0.0.0 will only bind to IPv4-enabled interfaces. However, if you bind to ::, that should cover all IPv4 and IPv6 interfaces, assuming your TCP/IP stack (and Java) have IPv4-compatible IPv6 sockets enabled.

You'll need to look to the kernel (or socket libraries, if you're on Windows) for an explanation of "why". On my OS X system, the man pages explain it.

From man 4 inet`:

Sockets may be created with the local address INADDR_ANY to effect 'wildcard' matching on incoming messages. The address in a connect(2) or sendto(2) call may be given as INADDR_ANY to mean 'this host'. The distinguished address INADDR_BROADCAST is allowed as a shorthand for the broadcast address on the primary network if the first network configured supports broadcast.

From man 4 inet6:

Sockets may be created with the local address :: (which is equal to IPv6 address 0:0:0:0:0:0:0:0) to affect 'wildcard' matching on incoming messages.

user207421
  • 305,947
  • 44
  • 307
  • 483
mpontillo
  • 13,559
  • 7
  • 62
  • 90
9

It has nothing to do with Java. 0.0.0.0 is INADDR_ANY, which is is a special address that is guaranteed to receive from any network interface by the C sockets API, which is called by Java.

user207421
  • 305,947
  • 44
  • 307
  • 483
3

It seems to be a special reserved IP address of sorts. This link probably has more information Is 0.0.0.0 a valid IP address?. So I suspect it is not documented in Java as it is more related to the actual network specification.

And as some others have mentioned it appears that it is the Chuck Norris IP address :D

HTH

Community
  • 1
  • 1
Namphibian
  • 12,046
  • 7
  • 46
  • 76