Duplicate packets in Multicast Receiver Socket [duplicate]

有些话、适合烂在心里 提交于 2019-12-04 10:23:08

One approach you can take is to be smart about how you join the groups, so rather than create a socket, bind (with REUSEADDR) and then join group for each pair of ip, port, only construct a single socket and bind to a given port, and then issue multiple IGMP joins on the same socket.

i.e. in your case, only one socket is created, you bind once per port, but you join multiple groups. The only difference is that when you issue the read call, you will get a packet from one or the other group and you need to have enough data in the packet to allow your to distinguish.

Helius

Replace

mcast_Addr.sin_addr.s_addr = htonl(INADDR_ANY);

with

mcast_Addr.sin_addr.s_addr = inet_addr (mc_addr_str);

it's help for me (linux), for each application i receive separate mcast stream from separate mcast group on one port.

Also you can look into VLC player source, it show many mcast iptv channel from different mcast group on one port, but i dont know, how it separetes channel.

I'm guessing this is because of multiple interfaces (you join the group on INADDR_ANY). Try specifying exact interface. Get the interface address via ioctl(2) with SIOCGIFADDR. Check what groups you joined on what interface with netstat -ng.

It's a feature of Linux routing, you need a unique port/multicast group for each session, Linux will forward on anything as long as the port matches, for example broadcast packets too. Windows surprisingly doesn't have this symptom, which is presumably why it is slower.

Many commercial middleware packages enforce this requirement for compatibility, for example TIBCO's Rendezvous will not permit reusing of the same port or group.

Have you tried just turning loopback off? I find that if I have a reasonable TTL, no loopback is required to get a single, at least when using SO_REUSEPORT:

int sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( sock < 0 )
    exit( -11 );

int on = true;
if( setsockopt ( sock, SOL_SOCKET, SO_REUSEPORT, & on, sizeof( on ) ) < 0 ) 
    exit( -12 );

int off = 0;
if ( setsockopt ( sock, IPPROTO_IP, IP_MULTICAST_LOOP, & off, sizeof( off ) ) < 0 )
    exit( -13 );

int ttl = 3;
if ( setsockopt ( sock, IPPROTO_IP, IP_MULTICAST_TTL, & ttl, sizeof( ttl ) ) < 0 )
    exit( -14 );

If I leave loopback turned on--as it is by default--I get double packets too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!