#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/ip_icmp.h> #include <netdb.h> #define PACKET_SIZE 4096 #define ERROR 0 #define SUCCESS 1 // 效验算法 unsigned short cal_chksum(unsigned short *addr, int len) { int nleft=len; int sum=0; unsigned short *w=addr; unsigned short answer=0; while(nleft > 1) { sum += *w++; nleft -= 2; } if( nleft == 1) { *(unsigned char *)(&answer) = *(unsigned char *)w; sum += answer; } sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); answer = ~sum; return answer; } // Ping函数 int ping( char *ips, int timeout) { struct timeval timeo; int sockfd; struct sockaddr_in addr;