1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#define IP_SIZE 20
#define ICMP_SIZE 16
#define MAX_PACKET 100
int main(int argc, char * argv[])
{
WSADATA WSAData;
//Initialisation de Winsock2
if(WSAStartup(MAKEWORD(2,2), & WSAData)!=0)
{perror("WSAStartup"); exit(1);}
char packet_buf[100];
struct sockaddr_in their_addr; // Adresse de celui qui se connecte
int sock_fd;
//création socket
if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{perror("socket"); exit(1);}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(80);
their_addr.sin_addr.s_addr=inet_addr("217.12.3.11");
//connexion au serveur
if (connect(sock_fd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr_in)) == -1)
{perror("connect"); exit(1);}
struct icmphdr *icmp_buf=(struct icmphdr *) packet_buf;
//fonction qui construit la trame ICMP de type Echo Request
icmp_buf=BuildTrame(icmp_buf);
//envoie d'une requête (ici, message ICMP de type Echo Request)
if (send(sock_fd, (char *)icmp_buf, 16, 0) == -1)
{perror("send"); exit(1);}
struct icmphdr * icmp = (struct icmphdr *) malloc(ICMP_SIZE); //allocation mémoire
/* Réception de la réponse ICMP avec l'entête IP */
do
{
fd_set readfds;
FD_ZERO(&readfds);
timeval tv;
tv.tv_sec=5;
tv.tv_usec=0;
FD_SET(sock_fd, &readfds);
if(select(sock_fd+1,&readfds,NULL,NULL,&tv)<0)
{perror("select"); exit(1);}
if(recv(sock_fd, packet_buf, IP_SIZE+ICMP_SIZE, 0)<0)
{perror("recv"); exit(1);}
else {
// Récupération de la partie ICMP du packet
memcpy(icmp, &packet_buf[IP_SIZE], ICMP_SIZE);
}
}while(icmp->type!=0); //ICMP_ECHO REPLY
struct time tps;
gettime((struct time *)&tps);
//Affiche l’heure au moment de la réception du message et l’heure
//contenue dans le message réceptionné et calcul leur différence.
double diff=diff_time(&tps,(struct time *)icmp->data);
cout<<""Délais : "<<diff<<" sec. "<<endl<<endl;
}//main |
Partager