在一个套接字上订阅多个多播组(Linux、C)
是否可以在单个套接字上从多个多播组接收数据?
例如:
void AddGroup(int sock,
const char* mc_addr_str,
int mc_port,
const char* interface) {
struct sockaddr_in mc_addr;
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family = AF_INET;
mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
mc_addr.sin_port = htons(mc_port);
if ((bind(sock, (struct sockaddr *) &mc_addr,
sizeof(mc_addr))) < 0) {
perror("bind() failed");
exit(1);
}
// construct an IGMP join request structure
struct ip_mreq mc_req;
mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
mc_req.imr_interface.s_addr = inet_addr(interface);
if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void*) &mc_req, sizeof(mc_req))) < 0) {
perror("setsockopt() failed");
exit(1);
}
}
当我添加一个多播组时,此代码有效。但是当我尝试添加另一个时,“绑定”失败。我不太明白为什么绑定首先需要存在? (但如果没有它,代码就无法工作)。
理想情况下,我想在同一个套接字上多次调用 AddGroup。这可能吗?或者我是否需要每组一个套接字,然后只使用轮询?
Is it possible to receive data from more than one multicast group on a single socket?
For example:
void AddGroup(int sock,
const char* mc_addr_str,
int mc_port,
const char* interface) {
struct sockaddr_in mc_addr;
memset(&mc_addr, 0, sizeof(mc_addr));
mc_addr.sin_family = AF_INET;
mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
mc_addr.sin_port = htons(mc_port);
if ((bind(sock, (struct sockaddr *) &mc_addr,
sizeof(mc_addr))) < 0) {
perror("bind() failed");
exit(1);
}
// construct an IGMP join request structure
struct ip_mreq mc_req;
mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
mc_req.imr_interface.s_addr = inet_addr(interface);
if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void*) &mc_req, sizeof(mc_req))) < 0) {
perror("setsockopt() failed");
exit(1);
}
}
This code works when I add one multicast group. But when I try to add another, the "bind" fails. I don't quite understand why the bind needs to be there in the first place? (but the code doesn't work without it).
Ideally I would like to call AddGroup multiple times on the same socket. Is this possible? Or do I need one socket per group and then just use polling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用带有 IP_ADD_MEMBERSHIP 选项的适当
setsockopt()
调用(而不是 bind())加入任意数量的多播组。You can join as many multicast groups as you like, using the appropriate
setsockopt()
call with the IP_ADD_MEMBERSHIP option, rather than bind().您只需绑定一个套接字一次。第二次跳过绑定,看看会发生什么。
You only bind a socket once. Skip the bind the second time and see what happens.
您可以在单个套接字上加入任意数量的多播组。请参阅setsockopt()、IP_PKTINFO 了解您正在从哪个多播组读取数据的方法。
You can join as many multicast groups you want to on a single socket. See setsockopt(), IP_PKTINFO for a way to recognize which multicast group you are reading data from.
绑定
到被动地址,即IPv4的0.0.0.0,并使用ASM或SSM来拉入其他组,例如列出的IP_ADD_MEMBERSHIP
。您只能
绑定
一次。bind
to the passive address, i.e. 0.0.0.0 for IPv4 and use ASM or SSM to pull in additional groups, e.g.IP_ADD_MEMBERSHIP
as listed.You can only
bind
once.是的,这是可能的:查看链接中的示例(http://www.tenouk.com/Module41c .html)
要通过几个步骤缩短此过程:
Yes, it's possible: look on the example in the link (http://www.tenouk.com/Module41c.html)
To shorten this up in a few steps:
在基于 UNIX 的操作系统中:
如果需要绑定到多播地址,则不能多次调用
bind()
。当您期望使用相同目标端口的多个多播流以及在同一设备中运行的多个进程接收这些多播时,您将需要绑定到多播地址。例如,当您有组播流:239.0.0.1:1234、239.0.0.2:1234、239.0.0.3:1234 和 239.0.0.4:1234,并且您希望在 process-A 中接收 239.0.0.1、239.0.0.2 和想要收到239.0.0.3、239.0.0.4 在进程 B 中,当进程 A 和 B 运行在同一设备中时,您无法完成此操作。
In unix based OSes:
If you need to bind to multicast address, you cannot call
bind()
more than once. And you will need to bind to multicast address when you expect more than one multicast streams using same destination port and multiple processes running in same device receiving those multicasts.For example, when you have multicast streams: 239.0.0.1:1234, 239.0.0.2:1234, 239.0.0.3:1234 and 239.0.0.4:1234, and you want to receive 239.0.0.1, 239.0.0.2 in process-A and want to receive 239.0.0.3, 239.0.0.4 in process-B, you cannot accomplish this when both processes A and B running in same device.