绑定多播(UDP)套接字是什么意思? [英] What does it mean to bind a multicast (UDP) socket?

查看:149
本文介绍了绑定多播(UDP)套接字是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在具有多个网络接口的主机之间使用多播UDP. 我正在使用boost :: asio,并且被接收者必须进行的2个操作混淆:绑定,然后加入组.

I am using multicast UDP between hosts that have multiple network interfaces. I am using boost::asio, and am confused by the 2 operations receivers have to make: bind, then join-group.

为什么在绑定期间对加入的每个多播组进行操作时,都需要指定接口的本地地址?

Why do you need to specify the local address of an interface, during bind, when you do that with every multicast group that you join?

姊妹问题涉及多播端口:由于在发送过程中,您将发送到多播地址&端口,为什么在订阅多播组期间仅指定地址,而不指定端口-在混乱的绑定中指定要绑定的端口.

The sister-question regards the multicast port: Since during sending, you send to a multicast address & port, why, during subscription to a multicast group, you only specify the address, not the port - the port being specified in the confusing call to bind.

注意:"join-group"是setsockopt(IP_ADD_MEMBERSHIP)的包装,如记录所示,可以在同一套接字上多次调用该包装以订阅不同的组(通过不同的网络?).因此,每次我订阅一个组时,放弃绑定调用并指定端口是很有意义的.

Note: the "join-group" is a wrapper over setsockopt(IP_ADD_MEMBERSHIP), which as documented, may be called multiple times on the same socket to subscribe to different groups (over different networks?). It would therefore make perfect sense to ditch the bind call and specify the port every time I subscribe to a group.

从我看到的情况来看,总是绑定到"0.0.0.0"并在加入组时指定接口地址,效果很好.糊涂了.

From what I see, always binding to "0.0.0.0" and specifying the interface address when joining the group, works very well. Confused.

推荐答案

在接收多播时绑定UDP套接字意味着指定接收数据的地址和端口(不像TCP接受器那样是本地接口)绑定).在这种情况下指定的地址具有过滤角色,即套接字将仅接收发送到该多播地址&的数据报.端口,无论套接字随后加入了什么组.这就解释了为什么绑定到INADDR_ANY(0.0.0.0)时,我会收到发送到多播组的数据报,而绑定到任何本地接口时,我却什么都没收到,即使数据报是在与该接口相连的网络上发送的也是如此.对应.

To bind a UDP socket when receiving multicast means to specify an address and port from which to receive data (NOT a local interface, as is the case for TCP acceptor bind). The address specified in this case has a filtering role, i.e. the socket will only receive datagrams sent to that multicast address & port, no matter what groups are subsequently joined by the socket. This explains why when binding to INADDR_ANY (0.0.0.0) I received datagrams sent to my multicast group, whereas when binding to any of the local interfaces I did not receive anything, even though the datagrams were being sent on the network to which that interface corresponded.

引自UNIX®网络编程第1卷,第三版:W.R史蒂文斯(W.R Stevens)的套接字网络API. 21.10.发送和接收

Quoting from UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API by W.R Stevens. 21.10. Sending and Receiving

[...]我们希望接收套接字绑定多播组,并且 端口,例如239.255.1.2端口8888.(回想一下,我们可以将 通配符IP地址和端口8888,但绑定多播地址 防止套接字接收任何其他可能的数据报 到达目的地为8888的端口.)然后,我们希望接收套接字 加入组播组.发送套接字将数据报发送到 相同的多播地址和端口,例如239.255.1.2端口8888.

[...] We want the receiving socket to bind the multicast group and port, say 239.255.1.2 port 8888. (Recall that we could just bind the wildcard IP address and port 8888, but binding the multicast address prevents the socket from receiving any other datagrams that might arrive destined for port 8888.) We then want the receiving socket to join the multicast group. The sending socket will send datagrams to this same multicast address and port, say 239.255.1.2 port 8888.

这篇关于绑定多播(UDP)套接字是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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