ping 程序的实现
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/ip_icmp.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#define PACKET_SIZE 4096
const int MAX_TIME_WAIT = 10;
const int maxpackage = 8;
struct sockaddr_in addr;
struct sockaddr_in from;
struct hostent *host;
int sendnum = 0, recvnum = 0;
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int datalen = 56;
struct timeval recvtime;
void subtime(struct timeval *in, struct timeval *out) ;
double sumtime;
int socketfd;
unsigned short check_sum(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) {
*(