Why are (UDP multicast) packets not being received?

自作多情 提交于 2019-12-03 09:45:01

I'm assuming that this is the recv side... On the face the multicast socket setup looks fine. You say it works on the simulator but not a real network, correct? There is an issue that your network equipment, specifically any routers, possibly other equipment as well may need to be explicitly set up to allow forwarding of broadcast and / or multicast packets. These kinds of packet are usually dropped at the edge of networks by default. Here's another long shot - if you run both the sender and receiver on the same machine and turn off IP_MULTICAST_LOOP then you won't get any packets as it disables the multicast loopback interface. That's all I can think of without more info on your setup and / or seeing a bit more code.

I needed to change the INADDR_ANY to the broadcastIP...

struct sockaddr_in addressData;
memset(&addressData, 0, sizeof(addressData));
addressData.sin_len = sizeof(addressData);
addressData.sin_family = AF_INET;
addressData.sin_port = htons(broadcastPort);
addressData.sin_addr.s_addr = inet_addr(broadcastIP);
NSData *address = [NSData dataWithBytes:&addressData length:sizeof(addressData)];
if (kCFSocketSuccess != CFSocketSetAddress(advertiseSocket, (CFDataRef)address)) {
    [self stopBeforeStart];
    [self connectionFailed];
    return;
}

struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(broadcastIP);         
mreq.imr_interface.s_addr = INADDR_ANY;

I had a similar problem trying to test on my PC, by tcpreplay, some data exchange recorded by Wireshark between other 2 PC's A and B. So I adapt the old record this way:

tcprewrite --pnat=A-IP:loIP,B-IP:loIP -i oldrecordfile -o newrecordfile

and then

tcpreplay -T nano --verbose -i lo newrecordfile

But my application recv() fail.

The problem could be related to loopback limit of tcpreplay, so I decide to resend the data from PC to PC, after regenerating suitable recordfile with new IP's by tcprewrite. At this point tcpdump showed things as excepted on my receiving-end, but the program recv() always fail.

Finally I found out the reason of that were the old MAC addresses and the presence of a router between the two PC's:

tcprewrite --pnat=oldA-IP:newA-IP,oldB-IP:newB-IP --enet-smac=newA-MAC,newA-MAC --enet-dmac=newB-MAC,newB-MAC -i oldrecordfile -o newrecordfile
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!