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

查看:34
本文介绍了绑定多播 (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 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天全站免登陆