【发布时间】:2021-12-09 19:27:38
【问题描述】:
我在网上搜索了很长时间。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。
程序写完了,今天测试ping环回地址,发送数据包后,recvfrom()函数收到“第一个”数据包(类型8),第二个recvfrom()收到响应数据包(类型 0)。
后来发现奇数次的type值为8,偶数次的type值为0。
我用Wireshark抓到的实际包每次都有对应的响应包,但是recvfrom()第一次收到的是传出的包。
// Send
if (sendto(sockfd, &sendicmp, ICMP_SIZE, 0, (struct sockaddr *) &to, sizeof(to)) == -1) {
printf("sendto() error \n");
continue;
}
// Receive
struct timeval timeout = {3, 0};//3s
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
if ((n = recvfrom(sockfd, buf, BUF_SIZE, 0, (struct sockaddr *) &from, &fromlen)) < 0) {
printf("Time out! \n");
continue;
}
nreceived++;
if (unpack(buf, n) == -1) {
printf("unpack() error \n");
}
enter image description here Since the type value is not 0, I let the output is not the ICMP packet sent to me
【问题讨论】:
-
你是如何设置套接字的?更一般地说,提供minimal reproducible example 怎么样?