if( (n=recvfrom(sockfd,recvpacket,sizeof(recvpacket),0,
(struct sockaddr *)&from,&fromlen)) <0)
{ if(errno==EINTR)continue;
perror("recvfrom error");
continue;
}
gettimeofday(&tvrecv,NULL); /*記錄接收時間*/
if(unpack(recvpacket,n)==-1)continue;
nreceived++;
}
}
/*剝去ICMP報頭*/
int unpack(char *buf,int len)
{ int i,iphdrlen;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
double rtt;
ip=(struct ip *)buf;
iphdrlen=ip->ip_hl<<2; /*求ip報頭長度,即ip報頭的長度標志乘4*/
icmp=(struct icmp *)(buf+iphdrlen); /*越過ip報頭,指向ICMP報頭*/
len-=iphdrlen; /*ICMP報頭及ICMP數據報的總長度*/
if( len<8) /*小于ICMP報頭長度則不合理*/
{ printf("ICMP packets\ s length is less than 8\n");
return -1;
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/