我正在编写一个LAN设备检测测试,该测试可以向多播组239.255.255.250发送请求并从同一LAN中的设备获得响应,
Wireshark告诉我,加入组(IGMPv2)很好,发送(UDP)很好,接收(UDP)很好,但是在我的代码中,当recvfrom()时它始终返回-1,无论我尝试了什么,总是-1 ... 我曾经在Windows中尝试过3rd party应用,它可以正常发送和接收。
有人能给我一个线索吗?提前谢谢。
socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(socket, SOL_SOCKET, SO_IFDEVICE, &i8IfIndex, sizeof(i8IfIndex));
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &u8On, sizeof(int));
/*all aboved checked alright*/
/*join to multicast group*/
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = htonl(0xEFFFFFFA); /*239.255.255.250*/
mreq.imr_interface.s_addr = INADDR_ANY;
setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
memset(&socket_address, 0, sizeof(socket_address));
socket_address.sin_addr.s_addr = htonl(0xEFFFFFFA);
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(37020);
//no help:bind(socket, (PSA) &socket_address, sizeof(socket_address));
memset(&socket_address2, 0, sizeof(socket_address2));
socket_address2.sin_addr.s_addr = INADDR_ANY;
socket_address2.sin_family = AF_INET;
socket_address2.sin_port = 0;
/*send & recv*/
int8 i8Result = 0xcc;
char *inquryStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Probe><Uuid>232B1234-FBcC-48B5-AC06-AE1F77051280</Uuid><Types>inquiry</Types></Probe>";
if(i8Result = sendto(socket, inquryStr, strlen(inquryStr), 0, (PSA) &socket_address, sizeof(socket_address))) {
//wireshark detected ,i8Result shows 0x7C sent
if(i8Result = recvfrom(socket, g_HjtOsdComm.u8TRBuf, 1024, 0, (PSA) &socket_address2, sizeof(socket_address2))) {
//wireshark detected ,but i8Result get-1
}
}
/*leave group*/
setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
fdClose(HjtOsdSocket);