如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?

Why DatagramSocketImpl joinGroup method takes a NetworkInterface if the socket must have already been bound to it?(如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?)

本文介绍了如果套接字必须已经绑定到它,为什么 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.

这篇关于如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如果套接字必须已经绑定到它,为什么 DatagramSocketImpl joinGroup 方法需要一个 NetworkInterface?

基础教程推荐