附源码:平。cpp ping.h ,,,是类的实现。
实例代码是从项目的应用中剥离出来的:
平。cpp:
# include “ping.h” 萍:Ping (), { m_maxPacketSize =, 4; 时间=m_datalen 56; 时间=m_nsend 0; 时间=m_nreceived 0; 时间=m_icmp_seq 0; }/*校验和算法*/unsigned short 萍::getChksum (unsigned short * addr, int 兰) {,, int nleft=兰; int 金额=0; unsigned short * w=addr; unsigned short 回答=0;/*把ICMP报头二进制数据以2字节为单位累加起来*/而(nleft> 1) {,, +=* w + +总和; nleft -=2; }/*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这2个字节数据的低字节为0,继续累加*/如果,nleft==1) {,, * (unsigned char *)(及回答)=* w (unsigned char *); 和+=答案; } 金额=(,sum>的在16)+ (sum& 0 xffff); 和+=(,sum>的在16); 回答=~总和; return 回答; }/*设置ICMP报头*/int 平::packIcmp (int pack_no, struct icmp *, icmp) {,, packsize int 我; struct icmp * picmp; struct timeval * tval; picmp =, icmp; picmp→icmp_type=ICMP_ECHO; picmp→icmp_code=0; picmp→icmp_cksum=0; picmp→icmp_seq=pack_no; picmp→icmp_id=, m_pid; packsize=, 8, +, m_datalen; tval=, (struct timeval *) icmp→icmp_data; gettimeofday (tval, NULL),,,,,/*记录发送时间*/picmp→icmp_cksum=getChksum ((unsigned short *) icmp, packsize);,/*校验算法*/return packsize; }/*剥去ICMP报头*/bool 平::unpackIcmp (char * buf int len,, struct IcmpEchoReply * icmpEchoReply) {,, iphdrlen int 我; struct ip * ip; struct icmp * icmp; struct timeval * tvsend, tvrecv,, tvresult; double rtt; ip =, (struct ip *)缓冲区; 时间=iphdrlen ip→ip_hl & lt; & lt;, 2,,,,,/*求ip报头长度,即ip报头的长度标志乘4 */icmp =, (struct icmp *)(时间+ buf iphdrlen);,,/*越过ip报头,指向ICMP报头*/len -=, iphdrlen;,,,,,,,,,,,,/* ICMP报头及ICMP数据报的总长度*/如果(len & lt;, 8),,,,,,,,,,,,,,,,/*小于ICMP报头长度则不合理*/{,, printf (" ICMP 数据包\ ' s length  is less than 8 \ n”); return 假; }/*确保所接收的是我所发的的ICMP的回应*/如果(,(icmp→icmp_type==ICMP_ECHOREPLY),,,, (icmp→icmp_id ==, m_pid),) {,, tvsend=(struct timeval *) icmp→icmp_data; gettimeofday(和tvrecv, NULL);,,/*记录接收时间*/时间=tvresult tvSub (tvrecv, * tvsend);,,/*接收和发送的时间差*/rtt=tvresult.tv_sec * 1000, +, tvresult.tv_usec/1000,,,/*以毫秒为单位计算rtt */内河货运时间=icmpEchoReply→rtt ; icmpEchoReply→icmpSeq =, icmp→icmp_seq; 时间=icmpEchoReply→ipTtl ip→ip_ttl; icmpEchoReply→icmpLen =, len; return 真实; } else { return 假; } }/*两个timeval结构相减*/struct timeval 萍::tvSub (struct timeval  timeval1, struct timeval timeval2) {, struct timeval 结果; 时间=result timeval1; if (result.tv_usec & lt;, timeval2.tv_usec & lt;, 0) {,,, ——result.tv_sec; +=result.tv_usec 1000000; } result.tv_sec -=, timeval2.tv_sec; return 结果; }/*发送三个ICMP报文*/bool 平::sendPacket () {,, size_t packetsize; 而(,m_nsend & lt;, m_maxPacketSize) {,, m_nsend + +; m_icmp_seq + +; 时间=packetsize packIcmp (m_icmp_seq, (struct icmp *) m_sendpacket);,/*设置icmp报头*/如果(sendto (m_sockfd、m_sendpacket, packetsize,, 0,, (struct sockaddr *),和m_dest_addr,, sizeof (m_dest_addr)), & lt;, 0,,) {,, perror (“sendto 错误”); 继续; } } return 真实; }/*接收所有ICMP报文*/bool 平:recvPacket (PingResult , pingResult) {,,,,,, int 兰; extern int  errno; struct IcmpEchoReply  icmpEchoReply; int maxfds =, m_sockfd +, 1; int nfd ,=, 0; fd_set 资源集,,,,,, FD_ZERO(和资源集); socklen_t fromlen =, sizeof (m_from_addr); struct timeval 超时; timeout.tv_sec =, 4; 时间=timeout.tv_usec 0; 时间=for (int recvCount 0;, recvCount & lt;, m_maxPacketSize;, recvCount + +), {,,//printf (" begin recv \ n "); FD_SET (m_sockfd,和资源集); if ((=nfd 选择(maxfds,,,资源集,,空,,空,,,超时)),==,1),{,,,,,,,,,,,, perror (“select 错误”); 继续,,,, } if (nfd ==, 0), { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullarm linux c/c++实现平网络测试