在同一端口上接收多个多播提要

编程入门 行业动态 更新时间:2024-10-28 13:15:31
本文介绍了在同一端口上接收多个多播提要-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 ,此讨论包含测试程序以重现该问题并加以解决.

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.

更多推荐

在同一端口上接收多个多播提要

本文发布于:2023-08-07 03:53:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   端口   提要   在同一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!