Why DatagramSocketImpl joinGroup method takes a NetworkInterface if the socket must have already been bound to it?(如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?)
问题描述
只是好奇.那是多余的吗?您还没有绑定到您要使用的网络吗?当您绑定到 0.0.0.0 并且现在只想从接口 X 收听多播数据包时,也许就是这种情况?
Just curious. Is that redundant? Haven't you already bound to the network you want to use? Perhaps that's for the case when you bound to 0.0.0.0 and now want to listen to multicast packets only from interface X?
推荐答案
如果您绑定到 INADDR_ANY(这是正常情况),则加入组 IGMP 消息会通过路由表所说的提供到多播地址的最短路由的任何 NIC 接口发出.在多宿主主机中,您可能需要它通过所有 NIC 出去,因此您可以循环它们依次通过每个 NIC 加入.
If you are bound to INADDR_ANY, which is the normal case, the join-group IGMP message goes out via whichever NIC interface the routing tables say gives the shortest route to the multicast address. In multi-homed hosts you may need it to go out via all NICs, so you loop over them joining via each in turn.
如果您绑定到特定的 NIC,则在加入时指定网络接口是没有意义的.
If you're bound to a specific NIC it doesn't make sense to specify a network interface when joining.
如果您绑定到多播地址本身,因此该套接字只能接收多播,而不是单播 UDP,如果您想通过所有接口加入,您可能需要像 (1) 中那样循环.
If you're bound to the multicast address itself, so that that socket can only receive multicasts, not unicast UDP, you may need to loop as in (1) if you want to join via all interfaces.
这篇关于如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?


基础教程推荐
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01