在同一端口上接收多个多播源 - C、Linux [英] Receiving multiple multicast feeds on the same port - C, Linux

查看:39
本文介绍了在同一端口上接收多个多播源 - C、Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序正在从同一端口上的多个多播源接收数据.我能够接收数据.但是,我正在尝试考虑每个组的统计信息(即收到的消息,收到的字节)并且所有数据都被混淆了.有谁知道如何解决这个问题?如果我尝试查看发件人的地址,它不是多播地址,而是发件人机器的 IP.

I have an application that is receiving data from multiple multicast sources on the same port. I am able to receive the data. However, I am trying to account for statistics of each group (i.e. msgs received, bytes received) and all the data is getting mixed up. Does anyone know how to solved this problem? If I try to look at the sender's address, it is not the multicast address, but rather the IP of the sending machine.

我正在使用以下套接字选项:

I am using the following socket options:

struct ip_mreq mreq;         
mreq.imr_multiaddr.s_addr = inet_addr("224.1.2.3");         
mreq.imr_interface.s_addr = INADDR_ANY;         
setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

还有:

setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse));

推荐答案

在面对这种 linux 奇怪的行为几年后,并使用之前 答案,我意识到 ip(7) 联机帮助页 描述一个可能的解决方案:

After some years facing this linux strange behaviour, and using the bind workaround describe in previous answers, I realize that the ip(7) manpage describe a possible solution :

IP_MULTICAST_ALL (Linux 2.6.31 起)
此选项可用于修改多播消息到绑定到通配符 INADDR_ANY 的套接字地址.参数是一个布尔整数(默认为 1).如果设置为 1,套接字将接收来自所有在整个系统上全局加入的组.否则,它只会传递来自以下组的消息已明确加入(例如通过IP_ADD_MEMBERSHIP 选项)在这个特定的套接字上.

IP_MULTICAST_ALL (since Linux 2.6.31)
This option can be used to modify the delivery policy of multicast messages to sockets bound to the wildcard INADDR_ANY address. The argument is a boolean integer (defaults to 1). If set to 1, the socket will receive messages from all the groups that have been joined globally on the whole system. Otherwise, it will deliver messages only from the groups that have been explicitly joined (for example via the IP_ADD_MEMBERSHIP option) on this particular socket.

然后您可以使用以下方法激活过滤器以接收已加入群组的消息:

Then you can activate the filter to receive messages of joined groups using :

int mc_all = 0;
if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_ALL, (void*) &mc_all, sizeof(mc_all))) < 0) {
    perror("setsockopt() failed");
}

这个问题以及启用 IP_MULTICAST_ALL 的解决方法在 Redhat Bug 231899,此讨论包含重现问题并解决问题的测试程序.

This problem and the way to solve it enabling IP_MULTICAST_ALL is discussed in Redhat Bug 231899, this discussion contains test programs to reproduce the problem and to solve it.

这篇关于在同一端口上接收多个多播源 - C、Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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