本文主要是介绍组播应用层收不到问题-接收网卡绑定,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题背景:
平台:Android
设备A:组播发送者,发送组播数据
设备B:组播接受者,接收组播数据
设备A与设备B通过有线连接,连接在设备B的eth0网卡上
问题:
设备B在没有通讯模块网卡的情况下收得到设备A的组播数据,一旦有了通讯模块的拨号存在,则收不到A的组播数据了。但设备B上通过抓包,是能确认有抓到组播数据进来,也就是说eth0的网卡上,组播数据是进来了
组播注册伪代码:
int listenSock = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == listenSock){printf("Create socket failed! listenSock=%d\n", listenSock);return -1;}xint32_t opt = 1;
setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));addr.sin_family = AF_INET;
addr.sin_port = htons(static_cast<uint16_t>(端口));
addr.sin_addr.s_addr = INADDR_ANY;if (bind(listenSock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1){printf("Bind sock failed!!!\n");close(listenSock);return -1;}struct ip_mreq group;
group.imr_multiaddr.s_addr = inet_addr("组播ip");
group.imr_interface.s_addr = INADDR_ANY;if (setsockopt(listenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0){close(listenSock);printf("Add membership fail! errno:%d\n", errno);return -2;}struct ifreq nif = {0};strcpy(nif.ifr_name, "eth0");if (setsockopt(listenSock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&nif, sizeof(nif)) < 0){close(listenSock);printf("Bind interface=eth0 fail!, errno:%d\n", errno);return -1;}
通过以上代码,在存在通讯模块的时候是收不到的。也怀疑过是否是因为被添加了filter导致,后面也排除了,那么还有种可能,就是绑定网卡并没有生效,我们虽然socket绑定到了eth0,但组播的加入也涉及网卡,我们这个方式因顺序被加入到了其他网卡里,直接看修改,那么在添加组播组的时候也能绑定网卡,修改后的伪代码如下:
int listenSock = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == listenSock){printf("Create socket failed!!! listenSock=%d\n", m_listenSock);return -1;}xint32_t opt = 1;setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));struct sockaddr_in localAddr;
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
inet_pton(AF_INET, "组播ip", &localAddr.sin_addr);
localAddr.sin_port = htons((uint16_t)端口);
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);if (::bind(listenSock, reinterpret_cast<struct sockaddr*>(&localAddr), sizeof(localAddr)) == -1){printf("Bind sock failed!!!\n");close(listenSock);return -1;}struct ifreq nif;memset(&nif, 0, sizeof(ifreq));strcpy(nif.ifr_name, "eth0");if (::setsockopt(listenSock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&nif, sizeof(ifreq)) < 0){close(listenSock);printf("Bind interface=eth0 fail!, errno:%d\n", errno);return -1;}//关键修改在这struct ip_mreqn group;group.imr_multiaddr.s_addr = inet_addr("组播ip");group.imr_address.s_addr = htonl(INADDR_ANY);group.imr_ifindex = if_nametoindex("eth0");//在添加组的时候, 指定网卡if (::setsockopt(listenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0){close(listenSock);printf("Add membership interface=eth0 fail!, errno:%d\n", errno);return -2;}
也看到了,结构体用得也不一样了,其实 ip_mreq 与 ip_mreqn时差不多的含义
//来源:#include <in.h>
/* Internet address. */
struct in_addr {__be32 s_addr;
};/* Request struct for multicast socket ops */
struct ip_mreq {struct in_addr imr_multiaddr; /* IP multicast address of group */ //组播组组地址struct in_addr imr_interface; /* local IP address of interface */ // 组播组的的网卡ip, 对应的网卡能够接收对应多播组的数据包
};struct ip_mreqn {struct in_addr imr_multiaddr; /* IP multicast address of group */struct in_addr imr_address; /* local IP address of interface */ //组播组的的网卡ip, 对应的网卡能够接收对应多播组的数据包int imr_ifindex; /* Interface index */ //加入组播组的网卡index,优先级高于以上地址
};
可以看到以下,ip_mreqn明确多了一个网卡指定。我在使用ip_mreq的时候也尝试过将imr_interface指定为我eth0的ip来绑定,仍然无效。如果不是要收多个网卡的,可以使用以上方式修改,记录借鉴
这篇关于组播应用层收不到问题-接收网卡绑定的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!