我有一个示例代码来接收RedHat Enterprise Linux5.5中的broatcast数据包, 以下是关键来源:
struct ip_mreq {
struct in_addr imr_multiaddr; /* IP multicast address of group */
struct in_addr imr_interface; /* local IP address of interface */
} mreq;
serv_addr.sin_addr.s_addr = inet_addr("225.0.100.100");
portno = 10000 ;
serv_addr.sin_port = htons(portno);
mreq.imr_multiaddr = serv_addr.sin_addr;
mreq.imr_interface.s_addr = INADDR_ANY ;
//mreq.imr_interface.s_addr = inet_addr("192.168.165.212");
setsockopt(newsockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
setsockopt(newsockfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
路由表是:
[root @ linux01~] #route 内核IP路由表 目标网关Genmask标志指标参考使用Iface
192.172.165.0 * 255.255.255.0 U 0 0 0 eth0
128.110.10.0 * 255.255.255.0 U 0 0 0 eth2
225.0.0.0 192.172.165.212 255.255.0.0 UG 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
默认值128.110.10.254 0.0.0.0 UG 0 0 0 eth2
我使用tcpdump -i eth0 -nn来观察加入广播组是否成功,
mreq.imr_interface.s_addr = INADDR_ANY ;
//mreq.imr_interface.s_addr = inet_addr("192.168.165.212");
将展示:
16:14:24.322672 IP 192.172.165.212> 225.0.100.100:igmp v2报告225.0.100.100
mreq.imr_interface.s_addr = INADDR_ANY ;
//mreq.imr_interface.s_addr = inet_addr("192.168.165.212");
这将不会有igmp v2报告消息!!
我的问题是:为什么INADDR_ANY有效,但不是inet_addr(“192.168.165.212”);
我错过了什么?
eth0 inet addr:192.172.165.212 Bcast:192.172.165.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500公制:1
eth2 inet addr:128.110.10.69 Bcast:128.110.10.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500公制:1