在Linux中使用C++; 我用C++和原始套接字向路由器发送ICMP请求,之后我想读取ICMP回复。我的问题是,select()没有收到重播并且一直超时。我没有收到任何错误(errno返回success)。路由器正在发送ICMP回复,因为我可以使用Wireshark查看响应
wireshark截图 为了测试我的程序,我使用运行在VirtualBox 4.2.6和GN3上的Ubuntu12.10作为虚拟网络 我的源代码:在Linux中使用C++; 我用C++和原始套接字向路由器发送ICMP请求,之后我想读取ICMP回复。我的问题是,select()没有收到重播并且一直超时。我没有收到任何错误(errno返回success)。路由器正在发送ICMP回复,因为我可以使用Wireshark查看响应,c++,linux,select,icmp,C++,Linux,Select,Icmp,wireshark截图 为了测试我的程序,我使用运行在VirtualBox 4.2.6和GN3上的Ubuntu12.10作为虚拟网络 我的源代码: char buffer[IP_MAXPACKET]; // for the received ICMP reply struct iphdr *ipRec; // ICMP header timeval tv; // timeout fd_set mySet; // descriptor set ... tv.tv_sec = 3; // defau
char buffer[IP_MAXPACKET]; // for the received ICMP reply
struct iphdr *ipRec; // ICMP header
timeval tv; // timeout
fd_set mySet; // descriptor set
...
tv.tv_sec = 3; // default time-out 3s
tv.tv_usec = 0;
int retval; // select
...
do {
FD_ZERO(&mySet);
FD_SET(mysocket, &mySet);
retval = select(mysocket+1, &mySet, NULL, NULL, &tv);
cout << "Errno after select:" << strerror(errno) << endl;
if(retval == -1) {
cerr << "select error" << endl;
break;
}
else if (retval) {
if((length = recvfrom(mysocket, buffer, MAX, 0, result->ai_addr, &(result->ai_addrlen))) == -1) {
cerr << "Error: while receiving data." << endl;
}
else {
cout << "good" << endl;
ipRec = (struct iphdr*) buffer;
icmpRec = (struct icmphdr*) (buffer + ipRec->ihl * 4);
cout << "the packet." << " PID: " << ntohs(icmpRec->un.echo.id) << " Seq: " << ntohs(icmpRec->un.echo.sequence) << endl;
if ((icmpRec->type == ICMP_ECHOREPLY) && (ntohs(icmpRec->un.echo.id) == pid) && (ntohs(icmpRec->un.echo.sequence) == (seq - 1))) {
minBuff = lengthBuff;
}
}
} else {
// getting here all the time = select times-out and reads no data
cout << "mysocket:" << mysocket << endl;
cout << "retval:" << retval << endl;
maxBuff = lengthBuff;
break;
}
} while (!((icmpRec->type == ICMP_ECHOREPLY) && (ntohs(icmpRec->un.echo.id) == pid) && (ntohs(icmpRec->un.echo.sequence) == (seq - 1))));
if (packet)
delete(packet);
...
char缓冲区[IP_MAXPACKET];//对于收到的ICMP回复
结构iphdr*ipRec;//ICMP报头
timeval tv;//超时
fd_set mySet;//描述符集
...
tv.tv_sec=3;//默认超时3s
tv.tv_usec=0;
int retval;//选择
...
做{
FD_ZERO(&mySet);
FD_集(mysocket和mySet);
retval=select(mysocket+1和mySet、NULL、NULL和tv);
CUT< P >修正了问题。找到了不同线程的解决方案:改变IptoFixBooto到IptoPrimeICMP。现在是选择ICMP回复包。< / P>注意“是的,我的错,它明显的C++:”马克斯在RevVoD中是什么?这不是IPOxMax包吗?解决了问题。在不同的线程中找到了解决方案:W到IPPROTO_ICMP修复了它。现在选择读取ICMP回复数据包。MAX定义为IP_MAXPACKET。