我有在 RedHat Enterprise Linux5.5 中接收广播数据包的 ac 示例代码,以下是关键源:
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 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use 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 Metric:1
eth2 inet addr:128.110.10.69 Bcast:128.110.10.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1