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

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

问题描述

我有一个应用程序正在从同一端口上的多个多播源接收数据.我能够接收数据.但是,我试图考虑每个组的统计信息(即接收到的msgs,接收到的字节),并且所有数据都变得混乱起来.有谁知道如何解决这个问题?如果我尝试查看发件人的地址,它不是多播地址,而是发送方机器的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");
}

Redhat Bug 231899 <中讨论了启用IP_MULTICAST_ALL的问题和解决方法. /a>,此讨论包含测试程序以重现该问题并加以解决.

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天全站免登陆