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

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

问题描述

我凭经验发现

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

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

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.

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

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

推荐答案

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

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.

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

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.

来自man 4 inet`:

可以使用本地地址 INADDR_ANY 创建套接字以生效传入消息的通配符"匹配.connect(2) 中的地址或 sendto(2) 调用可以作为 INADDR_ANY 给出,表示这个主机".这允许区分地址 INADDR_BROADCAST 作为如果配置了第一个网络,则主网络上的广播地址支持广播.

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.

来自man 4 inet6:

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

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天全站免登陆