Skip to content
Advertisement

Is binding to 0.0.0.0 in Java guaranteed to bind to all network interfaces?

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement