在Java中绑定到0.0.0.0是否保证绑定到所有网络接口? [英] Is binding to 0.0.0.0 in Java guaranteed to bind to all network interfaces?

查看:109
本文介绍了在Java中绑定到0.0.0.0是否保证绑定到所有网络接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从经验上发现

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

绑定到当前计算机上的所有网络接口(而不仅仅是localhost - 127.0.0.1或主机名),但是我无法找到证明这是有保证的文档。



问题:在Java中定义到0.0.0.0的绑定总是在哪里绑定到所有网络接口?

解决方案

使用 0.0.0.0 只会绑定到启用IPv4的接口。但是,如果绑定到 :: ,则应该涵盖所有IPv4和IPv6接口,假设您的TCP / IP堆栈(和Java)启用了IPv4兼容的IPv6套接字。 / p>

您需要查看内核(或套接字库,如果您在Windows上)以获取为什么的解释。在我的OS X系统上, man 页面解释了它。



来自 man 4 inet


可以使用本地地址INADDR_ANY创建套接字,以在传入消息上实现
'通配符'匹配。 connect(2)
sendto(2)调用中的地址可以作为INADDR_ANY给出,表示此主机。如果配置的第一个网络
支持广播,则允许
区分地址INADDR_BROADCAST作为主网络上
广播地址的简写。


来自 man 4 inet6


可以创建套接字本地地址'::'(等于
IPv6地址0:0:0:0:0:0:0:0)以影响传入的
消息的通配符匹配。



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?

解决方案

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.

这篇关于在Java中绑定到0.0.0.0是否保证绑定到所有网络接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆