如果套接字必须已经绑定到它,为什么 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?

推荐答案

  1. 如果您绑定到 INADDR_ANY(这是正常情况),则加入组 IGMP 消息会通过路由表所说的提供到多播地址的最短路由的任何 NIC 接口发出.在多宿主主机中,您可能需要它通过所有 NIC 出去,因此您可以循环它们依次通过每个 NIC 加入.

  1. 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.

相关文章