From 3ad285eeca8cbc78580d3e7c962b2f831a5e583b Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 09:53:44 +0100 Subject: [PATCH 01/14] chore: remove tabs, indent all c files --- tsock_v0.c | 100 ++++----- tsock_v1.c | 310 ++++++++++++++-------------- tsock_v2.c | 496 ++++++++++++++++++++++----------------------- tsock_v3.c | 584 ++++++++++++++++++++++++++--------------------------- 4 files changed, 745 insertions(+), 745 deletions(-) diff --git a/tsock_v0.c b/tsock_v0.c index 168d258..ab70161 100644 --- a/tsock_v0.c +++ b/tsock_v0.c @@ -11,7 +11,7 @@ /* constantes et structures propres au domaine INTERNET */ #include /* structures retournées par les fonctions de gestion de la base de -données du réseau */ + données du réseau */ #include /* pour les entrées/sorties */ #include @@ -20,61 +20,61 @@ données du réseau */ void main (int argc, char **argv) { - int c; - extern char *optarg; - extern int optind; - int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ - int source = -1 ; /* 0=puits, 1=source */ - while ((c = getopt(argc, argv, "pn:s")) != -1) { - switch (c) { - case 'p': - if (source == 1) { - printf("usage: cmd [-p|-s][-n ##]\n"); - exit(1); - } - source = 0; - break; + int c; + extern char *optarg; + extern int optind; + int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ + int source = -1 ; /* 0=puits, 1=source */ + while ((c = getopt(argc, argv, "pn:s")) != -1) { + switch (c) { + case 'p': + if (source == 1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1); + } + source = 0; + break; - case 's': - if (source == 0) { - printf("usage: cmd [-p|-s][-n ##]\n"); - exit(1) ; - } - source = 1; - break; + case 's': + if (source == 0) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } + source = 1; + break; - case 'n': - nb_message = atoi(optarg); - break; + case 'n': + nb_message = atoi(optarg); + break; - default: - printf("usage: cmd [-p|-s][-n ##]\n"); - break; - } - } + default: + printf("usage: cmd [-p|-s][-n ##]\n"); + break; + } + } - if (source == -1) { - printf("usage: cmd [-p|-s][-n ##]\n"); - exit(1) ; - } + if (source == -1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } - if (source == 1) - printf("on est dans le source\n"); - else - printf("on est dans le puits\n"); + if (source == 1) + printf("on est dans le source\n"); + else + printf("on est dans le puits\n"); - if (nb_message != -1) { - if (source == 1) - printf("nb de tampons à envoyer : %d\n", nb_message); - else - printf("nb de tampons à recevoir : %d\n", nb_message); - } else { - if (source == 1) { - nb_message = 10 ; - printf("nb de tampons à envoyer = 10 par défaut\n"); - } else - printf("nb de tampons à envoyer = infini\n"); + if (nb_message != -1) { + if (source == 1) + printf("nb de tampons à envoyer : %d\n", nb_message); + else + printf("nb de tampons à recevoir : %d\n", nb_message); + } else { + if (source == 1) { + nb_message = 10 ; + printf("nb de tampons à envoyer = 10 par défaut\n"); + } else + printf("nb de tampons à envoyer = infini\n"); - } + } } diff --git a/tsock_v1.c b/tsock_v1.c index 3cde554..e3cce3d 100644 --- a/tsock_v1.c +++ b/tsock_v1.c @@ -11,7 +11,7 @@ /* constantes et structures propres au domaine INTERNET */ #include /* structures retournées par les fonctions de gestion de la base de -données du réseau */ + données du réseau */ #include /* pour les entrées/sorties */ #include @@ -28,167 +28,167 @@ void afficher_message(char *message, int lg) { int i; printf("message reçu : "); for (i=0;ih_addr, - hp->h_length ) ; - - for (int i = 0; i < nb_message; i++) { - // Construction du message - construire_message(message, 'a' + (i % 26), BASE_SIZE); - - // Envoi du message - - sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); - } - - exit(0); - - } - - else { - - - // Creation de l'adresse du socket distant - struct sockaddr_in adr_locale; - int longueur_adr_locale = sizeof(adr_locale); - - int longueur_message = BASE_SIZE; - - memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; - - adr_locale.sin_family=AF_INET; - adr_locale.sin_port=port; - adr_locale.sin_addr.s_addr = INADDR_ANY; - - // Bind the local socket to any local address (ie any available interface) - if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { - printf("failed to bind\n"); - exit(1); - } - - while (1) { - // Receive a single message because we are lazy - recvfrom(sock, message, longueur_message, 0, NULL, NULL); - - // Afficher notre seule et unique triste message - afficher_message(message, longueur_message); - } - - exit(0); - - - - } + while ((c = getopt(argc, argv, "pn:su")) != -1) { + switch (c) { + case 'p': + if (source == 1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1); } + source = 0; + break; + + case 's': + if (source == 0) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } + source = 1; + break; + + case 'n': + nb_message = atoi(optarg); + break; + + case 'u': + udp=1; + break; + + default: + printf("usage: cmd [-p|-s][-n ##]\n"); + break; + } + } + + char* nom_machine_distante; + + // Recuperation du port + int port=atoi(argv[argc-1]); + port = htons(port); + + char* message = malloc(BASE_SIZE * sizeof(char)); + + if (source == -1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } + + if (source == 1) { + printf("on est dans le source\n"); + + //Recuperation du nom logique + nom_machine_distante=argv[argc-2]; + } + + else + printf("on est dans le puits\n"); + + if (nb_message != -1) { + if (source == 1) + printf("nb de tampons à envoyer : %d\n", nb_message); + else + printf("nb de tampons à recevoir : %d\n", nb_message); + } else { + if (source == 1) { + nb_message = 10 ; + printf("nb de tampons à envoyer = 10 par défaut\n"); + } else + printf("nb de tampons à envoyer = infini\n"); + + } + + if (udp==1){ + // Creation du socket local + int sock= socket(AF_INET,SOCK_DGRAM,0); + + if (source==1) { + + + + // Creation de l'adresse du socket distant + struct hostent *hp ; + struct sockaddr_in adr_dest; + int longueur_adr_dest = sizeof(adr_dest); + + int longueur_message = BASE_SIZE; + + memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; + + adr_dest.sin_family=AF_INET; + adr_dest.sin_port=port; + + + if ((hp = gethostbyname(nom_machine_distante)) == NULL) { + printf("erreur gethostbyname\n") ; + exit(1) ; + } + + memcpy( (char*)&(adr_dest.sin_addr.s_addr), + hp->h_addr, + hp->h_length ) ; + + for (int i = 0; i < nb_message; i++) { + // Construction du message + construire_message(message, 'a' + (i % 26), BASE_SIZE); + + // Envoi du message + + sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); + } + + exit(0); + + } + + else { + + + // Creation de l'adresse du socket distant + struct sockaddr_in adr_locale; + int longueur_adr_locale = sizeof(adr_locale); + + int longueur_message = BASE_SIZE; + + memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; + + adr_locale.sin_family=AF_INET; + adr_locale.sin_port=port; + adr_locale.sin_addr.s_addr = INADDR_ANY; + + // Bind the local socket to any local address (ie any available interface) + if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { + printf("failed to bind\n"); + exit(1); + } + + while (1) { + // Receive a single message because we are lazy + recvfrom(sock, message, longueur_message, 0, NULL, NULL); + + // Afficher notre seule et unique triste message + afficher_message(message, longueur_message); + } + + exit(0); + + + + } + } } diff --git a/tsock_v2.c b/tsock_v2.c index 8588426..8aaff95 100644 --- a/tsock_v2.c +++ b/tsock_v2.c @@ -11,7 +11,7 @@ /* constantes et structures propres au domaine INTERNET */ #include /* structures retournées par les fonctions de gestion de la base de -données du réseau */ + données du réseau */ #include /* pour les entrées/sorties */ #include @@ -28,263 +28,263 @@ void afficher_message(char *message, int lg) { int i; printf("message reçu : "); for (i=0;ih_addr, - hp->h_length ) ; - - for (int i = 0; i < nb_message; i++) { - // Construction du message - construire_message(message, 'a' + (i % 26), BASE_SIZE); - - // Envoi du message - - sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); - } - - - } - - else { - - - // Creation de l'adresse du socket distant - struct sockaddr_in adr_locale; - int longueur_adr_locale = sizeof(adr_locale); - - int longueur_message = BASE_SIZE; - - memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; - - adr_locale.sin_family=AF_INET; - adr_locale.sin_port=port; - adr_locale.sin_addr.s_addr = INADDR_ANY; - - // Bind the local socket to any local address (ie any available interface) - if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { - printf("failed to bind\n"); - exit(1); - } - - while (1) { - // Receive a single message because we are lazy - recvfrom(sock, message, longueur_message, 0, NULL, NULL); - - // Afficher notre seule et unique triste message - afficher_message(message, longueur_message); - } - - - - - } + while ((c = getopt(argc, argv, "pn:su")) != -1) { + switch (c) { + case 'p': + if (source == 1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1); } + source = 0; + break; - else{ - - // Creation du socket local - int sock= socket(AF_INET,SOCK_STREAM,0); - - if (source==1) { - - - - // Creation de l'adresse du socket distant - struct hostent *hp ; - struct sockaddr_in adr_dest; - int longueur_adr_dest = sizeof(adr_dest); - - int longueur_message = BASE_SIZE; - - memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; - - adr_dest.sin_family=AF_INET; - adr_dest.sin_port=port; - - - if ((hp = gethostbyname(nom_machine_distante)) == NULL) { - printf("erreur gethostbyname\n") ; - exit(1) ; - } - - memcpy( (char*)&(adr_dest.sin_addr.s_addr), - hp->h_addr, - hp->h_length ) ; - - // Demande de connexion - int succ; - if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){ - printf("Echec connect"); - exit(1); - } - - // Envoie des messages - for (int i = 0; i < nb_message; i++) { - // Construction du message - construire_message(message, 'a' + (i % 26), BASE_SIZE); - - // Envoi du message - write(sock,message,longueur_message); - } - - // Close socket to avoid dangling connections - close(sock); - - - } - - else { - - - // Creation de l'adresse du socket local - struct sockaddr_in adr_locale; - socklen_t longueur_adr_locale = sizeof(adr_locale); - - int longueur_message = BASE_SIZE; - - memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; - - adr_locale.sin_family=AF_INET; - adr_locale.sin_port=port; - adr_locale.sin_addr.s_addr = INADDR_ANY; - - // Bind the local socket to any local address (ie any available interface) - if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { - printf("failed to bind\n"); - exit(1); - } - - // set linstening queue size - if (listen(sock, 5) == -1) { - printf("échec et mat listen\n"); - exit(1); - } - - int sock_bis; - // Accept a single connection on the main thread - if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){ - printf("échec du accept\n") ; - exit(1) ; - } - - while (read(sock_bis, message, longueur_message) > 0) { - afficher_message(message, longueur_message); - } - close(sock_bis); - close(sock); - - - - - } + case 's': + if (source == 0) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; } + source = 1; + break; - exit(0); + case 'n': + nb_message = atoi(optarg); + break; + + case 'u': + udp=1; + break; + + default: + printf("usage: cmd [-p|-s][-n ##]\n"); + break; + } + } + + char* nom_machine_distante; + + // Recuperation du port + int port=atoi(argv[argc-1]); + port = htons(port); + + char* message = malloc(BASE_SIZE * sizeof(char)); + + if (source == -1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } + + if (source == 1) { + //Recuperation du nom logique + nom_machine_distante=argv[argc-2]; + } else printf("PUITS:"); + + + + if (nb_message != -1) { + if (source == 1) + printf("nb de tampons à envoyer : %d\n", nb_message); + else + printf("nb de tampons à recevoir : %d\n", nb_message); + } else { + if (source == 1) { + nb_message = 10 ; + printf("nb de tampons à envoyer = 10 par défaut\n"); + } else + printf("nb de tampons à envoyer = infini\n"); + + } + + if (udp==1){ + // Creation du socket local + int sock= socket(AF_INET,SOCK_DGRAM,0); + + if (source==1) { + + + + // Creation de l'adresse du socket distant + struct hostent *hp ; + struct sockaddr_in adr_dest; + int longueur_adr_dest = sizeof(adr_dest); + + int longueur_message = BASE_SIZE; + + memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; + + adr_dest.sin_family=AF_INET; + adr_dest.sin_port=port; + + + if ((hp = gethostbyname(nom_machine_distante)) == NULL) { + printf("erreur gethostbyname\n") ; + exit(1) ; + } + + memcpy( (char*)&(adr_dest.sin_addr.s_addr), + hp->h_addr, + hp->h_length ) ; + + for (int i = 0; i < nb_message; i++) { + // Construction du message + construire_message(message, 'a' + (i % 26), BASE_SIZE); + + // Envoi du message + + sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); + } + + + } + + else { + + + // Creation de l'adresse du socket distant + struct sockaddr_in adr_locale; + int longueur_adr_locale = sizeof(adr_locale); + + int longueur_message = BASE_SIZE; + + memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; + + adr_locale.sin_family=AF_INET; + adr_locale.sin_port=port; + adr_locale.sin_addr.s_addr = INADDR_ANY; + + // Bind the local socket to any local address (ie any available interface) + if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { + printf("failed to bind\n"); + exit(1); + } + + while (1) { + // Receive a single message because we are lazy + recvfrom(sock, message, longueur_message, 0, NULL, NULL); + + // Afficher notre seule et unique triste message + afficher_message(message, longueur_message); + } + + + + + } + } + + else{ + + // Creation du socket local + int sock= socket(AF_INET,SOCK_STREAM,0); + + if (source==1) { + + + + // Creation de l'adresse du socket distant + struct hostent *hp ; + struct sockaddr_in adr_dest; + int longueur_adr_dest = sizeof(adr_dest); + + int longueur_message = BASE_SIZE; + + memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; + + adr_dest.sin_family=AF_INET; + adr_dest.sin_port=port; + + + if ((hp = gethostbyname(nom_machine_distante)) == NULL) { + printf("erreur gethostbyname\n") ; + exit(1) ; + } + + memcpy( (char*)&(adr_dest.sin_addr.s_addr), + hp->h_addr, + hp->h_length ) ; + + // Demande de connexion + int succ; + if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){ + printf("Echec connect"); + exit(1); + } + + // Envoie des messages + for (int i = 0; i < nb_message; i++) { + // Construction du message + construire_message(message, 'a' + (i % 26), BASE_SIZE); + + // Envoi du message + write(sock,message,longueur_message); + } + + // Close socket to avoid dangling connections + close(sock); + + + } + + else { + + + // Creation de l'adresse du socket local + struct sockaddr_in adr_locale; + socklen_t longueur_adr_locale = sizeof(adr_locale); + + int longueur_message = BASE_SIZE; + + memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; + + adr_locale.sin_family=AF_INET; + adr_locale.sin_port=port; + adr_locale.sin_addr.s_addr = INADDR_ANY; + + // Bind the local socket to any local address (ie any available interface) + if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { + printf("failed to bind\n"); + exit(1); + } + + // set linstening queue size + if (listen(sock, 5) == -1) { + printf("échec et mat listen\n"); + exit(1); + } + + int sock_bis; + // Accept a single connection on the main thread + if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){ + printf("échec du accept\n") ; + exit(1) ; + } + + while (read(sock_bis, message, longueur_message) > 0) { + afficher_message(message, longueur_message); + } + close(sock_bis); + close(sock); + + + + + } + } + + exit(0); } diff --git a/tsock_v3.c b/tsock_v3.c index 2ee4e8c..ed10db1 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -11,7 +11,7 @@ /* constantes et structures propres au domaine INTERNET */ #include /* structures retournées par les fonctions de gestion de la base de -données du réseau */ + données du réseau */ #include /* pour les entrées/sorties */ #include @@ -32,286 +32,286 @@ int count_digits(int lg); int main (int argc, char **argv) { - int c; - extern char *optarg; - extern int optind; - int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ - int source = -1 ; /* 0=puits, 1=source */ - int udp=0; /* 0=TCP, 1=UDP */ - int lg=-1; /* Longueur des messages à envoyer ou recevoir: par défaut 30 octets */ + int c; + extern char *optarg; + extern int optind; + int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ + int source = -1 ; /* 0=puits, 1=source */ + int udp=0; /* 0=TCP, 1=UDP */ + int lg=-1; /* Longueur des messages à envoyer ou recevoir: par défaut 30 octets */ - while ((c = getopt(argc, argv, "pn:sul:")) != -1) { - switch (c) { - case 'p': - if (source == 1) { - printf("usage: cmd [-p|-s][-n ##]\n"); - exit(1); - } - source = 0; - break; + while ((c = getopt(argc, argv, "pn:sul:")) != -1) { + switch (c) { + case 'p': + if (source == 1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1); + } + source = 0; + break; - case 's': - if (source == 0) { - printf("usage: cmd [-p|-s][-n ##]\n"); - exit(1) ; - } - source = 1; - break; + case 's': + if (source == 0) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } + source = 1; + break; - case 'n': - nb_message = atoi(optarg); - // Packets are numerotated with 5 figures, which means the number of - // packets send can't be bigger than (or equal) 10 ** 6 - if(nb_message >= 100000) { - printf("Too many packets"); - exit(1); - } - - break; - - case 'u': - udp=1; - break; - - case 'l': - lg=atoi(optarg); - // A TCP packet is roughly 1500 bytes. We allow for some overhead. - // We provide an MTU of 1400 bytes. - if(lg >= 1400) { - printf("Messages too long"); - exit(1); - } - break; - - default: - printf("usage: cmd [-p|-s][-n ##]\n"); - break; - } - } - - char* nom_machine_distante; - - // Recuperation du port - int port=atoi(argv[argc-1]); - //port = htons(port); - - // Default set of packet length : 30 bytes - if (lg == -1) { - lg=30; - } - - //Allocation du message, il est de la taille donnée en paramètre - char* message = malloc(lg * sizeof(char)); - - - if (source == -1) { - printf("usage: cmd [-p|-s][-n ##]\n"); - exit(1) ; - } - - // If number of messages is not fixed for the source, it is set to 10 messages - if (nb_message == -1 && source ) { - nb_message = 10 ; - } - - - //Affichage des informations de communication initiée - if (source == 1) { - //Recuperation du nom logique - nom_machine_distante=argv[argc-2]; - printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s,dest=%s\n", - lg,port,nb_message,(udp)?"UDP":"TCP",nom_machine_distante); - } else { - printf("PUITS:lg_mesg-lu=%d,port=%d,nb_receptions=",lg,port); - if (nb_message!=-1 && !source) { - printf("infini"); - } - else { - printf("%d",nb_message); - } - printf(", TP=%s\n",(udp)?"UDP":"TCP"); - } - - - // Communnication through UDP messages - if (udp==1){ - // Creation du socket local - int sock= socket(AF_INET,SOCK_DGRAM,0); - - if (source==1) { - - - - // Creation de l'adresse du socket distant - struct hostent *hp ; - struct sockaddr_in adr_dest; - int longueur_adr_dest = sizeof(adr_dest); - - int longueur_message = lg; - - memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; - - adr_dest.sin_family=AF_INET; - adr_dest.sin_port=port; - - - if ((hp = gethostbyname(nom_machine_distante)) == NULL) { - printf("erreur gethostbyname\n") ; - exit(1) ; - } - - memcpy( (char*)&(adr_dest.sin_addr.s_addr), - hp->h_addr, - hp->h_length ) ; - - for (int i = 0; i < nb_message; i++) { - // Construction du message - construire_message(message, 'a' + (i % 26), lg); - - // Envoi du message - afficher_message_envoi(message,lg,i+1); - sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); - } - - - } - - else { - - - // Creation de l'adresse du socket distant - struct sockaddr_in adr_locale; - int longueur_adr_locale = sizeof(adr_locale); - - memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; - - adr_locale.sin_family=AF_INET; - adr_locale.sin_port=port; - adr_locale.sin_addr.s_addr = INADDR_ANY; - - // Bind the local socket to any local address (ie any available interface) - if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { - printf("failed to bind\n"); - exit(1); - } - - // Suivi du numéro de message reçu - int k=0; - - while (1) { - // Receive a single message because we are lazy - recvfrom(sock, message, lg, 0, NULL, NULL); - k++; - // Afficher notre seule et unique triste message - afficher_message_reception(message, lg, k); - } - - - - - } + case 'n': + nb_message = atoi(optarg); + // Packets are numerotated with 5 figures, which means the number of + // packets send can't be bigger than (or equal) 10 ** 6 + if(nb_message >= 100000) { + printf("Too many packets"); + exit(1); } - else{ - - // Creation du socket local - int sock= socket(AF_INET,SOCK_STREAM,0); + break; - if (source==1) { + case 'u': + udp=1; + break; - - - // Creation de l'adresse du socket distant - struct hostent *hp ; - struct sockaddr_in adr_dest; - int longueur_adr_dest = sizeof(adr_dest); - - int longueur_message = lg; - - memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; - - adr_dest.sin_family=AF_INET; - adr_dest.sin_port=port; - - - if ((hp = gethostbyname(nom_machine_distante)) == NULL) { - printf("erreur gethostbyname\n") ; - exit(1) ; - } - - memcpy( (char*)&(adr_dest.sin_addr.s_addr), - hp->h_addr, - hp->h_length ) ; - - // Demande de connexion - int succ; - if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){ - printf("Echec connect"); - exit(1); - } - - // Envoi des messages - for (int i = 0; i < nb_message; i++) { - // Construction du message - construire_message(message, 'a' + (i % 26), lg); - afficher_message_envoi(message,lg,i+1); - // Envoi du message - write(sock,message,longueur_message); - } - - // Close socket to avoid dangling connections - close(sock); - - - } - - else { - - - // Creation de l'adresse du socket local - struct sockaddr_in adr_locale; - socklen_t longueur_adr_locale = sizeof(adr_locale); - - int longueur_message = lg; - - memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; - - adr_locale.sin_family=AF_INET; - adr_locale.sin_port=port; - adr_locale.sin_addr.s_addr = INADDR_ANY; - - // Bind the local socket to any local address (ie any available interface) - if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { - printf("failed to bind\n"); - exit(1); - } - - // set linstening queue size - if (listen(sock, 5) == -1) { - printf("échec et mat listen\n"); - exit(1); - } - - int sock_bis; - // Accept a single connection on the main thread - if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){ - printf("échec du accept\n") ; - exit(1) ; - } - - // Suivi du numéro de message reçu - int k=0; - - while (read(sock_bis, message, longueur_message) > 0) { - afficher_message_reception(message, longueur_message,k); - k++; - } - close(sock_bis); - close(sock); - } + case 'l': + lg=atoi(optarg); + // A TCP packet is roughly 1500 bytes. We allow for some overhead. + // We provide an MTU of 1400 bytes. + if(lg >= 1400) { + printf("Messages too long"); + exit(1); } + break; - exit(0); + default: + printf("usage: cmd [-p|-s][-n ##]\n"); + break; + } + } + + char* nom_machine_distante; + + // Recuperation du port + int port=atoi(argv[argc-1]); + //port = htons(port); + + // Default set of packet length : 30 bytes + if (lg == -1) { + lg=30; + } + + //Allocation du message, il est de la taille donnée en paramètre + char* message = malloc(lg * sizeof(char)); + + + if (source == -1) { + printf("usage: cmd [-p|-s][-n ##]\n"); + exit(1) ; + } + + // If number of messages is not fixed for the source, it is set to 10 messages + if (nb_message == -1 && source ) { + nb_message = 10 ; + } + + + //Affichage des informations de communication initiée + if (source == 1) { + //Recuperation du nom logique + nom_machine_distante=argv[argc-2]; + printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s,dest=%s\n", + lg,port,nb_message,(udp)?"UDP":"TCP",nom_machine_distante); + } else { + printf("PUITS:lg_mesg-lu=%d,port=%d,nb_receptions=",lg,port); + if (nb_message!=-1 && !source) { + printf("infini"); + } + else { + printf("%d",nb_message); + } + printf(", TP=%s\n",(udp)?"UDP":"TCP"); + } + + + // Communnication through UDP messages + if (udp==1){ + // Creation du socket local + int sock= socket(AF_INET,SOCK_DGRAM,0); + + if (source==1) { + + + + // Creation de l'adresse du socket distant + struct hostent *hp ; + struct sockaddr_in adr_dest; + int longueur_adr_dest = sizeof(adr_dest); + + int longueur_message = lg; + + memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; + + adr_dest.sin_family=AF_INET; + adr_dest.sin_port=port; + + + if ((hp = gethostbyname(nom_machine_distante)) == NULL) { + printf("erreur gethostbyname\n") ; + exit(1) ; + } + + memcpy( (char*)&(adr_dest.sin_addr.s_addr), + hp->h_addr, + hp->h_length ) ; + + for (int i = 0; i < nb_message; i++) { + // Construction du message + construire_message(message, 'a' + (i % 26), lg); + + // Envoi du message + afficher_message_envoi(message,lg,i+1); + sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); + } + + + } + + else { + + + // Creation de l'adresse du socket distant + struct sockaddr_in adr_locale; + int longueur_adr_locale = sizeof(adr_locale); + + memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; + + adr_locale.sin_family=AF_INET; + adr_locale.sin_port=port; + adr_locale.sin_addr.s_addr = INADDR_ANY; + + // Bind the local socket to any local address (ie any available interface) + if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { + printf("failed to bind\n"); + exit(1); + } + + // Suivi du numéro de message reçu + int k=0; + + while (1) { + // Receive a single message because we are lazy + recvfrom(sock, message, lg, 0, NULL, NULL); + k++; + // Afficher notre seule et unique triste message + afficher_message_reception(message, lg, k); + } + + + + + } + } + + else{ + + // Creation du socket local + int sock= socket(AF_INET,SOCK_STREAM,0); + + if (source==1) { + + + + // Creation de l'adresse du socket distant + struct hostent *hp ; + struct sockaddr_in adr_dest; + int longueur_adr_dest = sizeof(adr_dest); + + int longueur_message = lg; + + memset((char *)& adr_dest, 0, sizeof(adr_dest)) ; + + adr_dest.sin_family=AF_INET; + adr_dest.sin_port=port; + + + if ((hp = gethostbyname(nom_machine_distante)) == NULL) { + printf("erreur gethostbyname\n") ; + exit(1) ; + } + + memcpy( (char*)&(adr_dest.sin_addr.s_addr), + hp->h_addr, + hp->h_length ) ; + + // Demande de connexion + int succ; + if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){ + printf("Echec connect"); + exit(1); + } + + // Envoi des messages + for (int i = 0; i < nb_message; i++) { + // Construction du message + construire_message(message, 'a' + (i % 26), lg); + afficher_message_envoi(message,lg,i+1); + // Envoi du message + write(sock,message,longueur_message); + } + + // Close socket to avoid dangling connections + close(sock); + + + } + + else { + + + // Creation de l'adresse du socket local + struct sockaddr_in adr_locale; + socklen_t longueur_adr_locale = sizeof(adr_locale); + + int longueur_message = lg; + + memset((char *)& adr_locale, 0, sizeof(adr_locale)) ; + + adr_locale.sin_family=AF_INET; + adr_locale.sin_port=port; + adr_locale.sin_addr.s_addr = INADDR_ANY; + + // Bind the local socket to any local address (ie any available interface) + if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) { + printf("failed to bind\n"); + exit(1); + } + + // set linstening queue size + if (listen(sock, 5) == -1) { + printf("échec et mat listen\n"); + exit(1); + } + + int sock_bis; + // Accept a single connection on the main thread + if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){ + printf("échec du accept\n") ; + exit(1) ; + } + + // Suivi du numéro de message reçu + int k=0; + + while (read(sock_bis, message, longueur_message) > 0) { + afficher_message_reception(message, longueur_message,k); + k++; + } + close(sock_bis); + close(sock); + } + } + + exit(0); } @@ -327,50 +327,50 @@ void afficher_message_envoi(char *message, int lg, int numero_envoi) { int nb_digits = count_digits(numero_envoi); for (int l=5; l>nb_digits; l--) { - printf("-"); + printf("-"); } printf("%d",numero_envoi); for (i=0;inb_digits; l--) { - printf("-"); + printf("-"); } printf("%d",numero_envoi); for (i=0;i 0) { - lg/=10; - retour++; - } - } + if (!lg){ + retour=1; + } + else { + retour=0; + while (lg > 0) { + lg/=10; + retour++; + } + } - return retour; + return retour; } From 0cd719fd823cb0d169a9b6d21b57a8e933032655 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 09:54:17 +0100 Subject: [PATCH 02/14] doc: compile all versions --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eacb257..63ba6f7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,16 @@ Compile version `X` : ``` -gcc -Wall -Wpedantic t_sockvX.c -o t_sockvX +gcc -Wall -Wpedantic tsock_vX.c -o t_sockvX +``` + +Or compile all versions: + +``` +gcc -Wall -Wpedantic tsock_v1.c -o t_sockv1 +gcc -Wall -Wpedantic tsock_v2.c -o t_sockv2 +gcc -Wall -Wpedantic tsock_v3.c -o t_sockv3 +gcc -Wall -Wpedantic tsock_v4.c -o t_sockv4 ``` Generate zip archive (replace `TP1` with relevant indication) : From 17a1935a540422dfd702d92a102911352cecb5d0 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 09:56:41 +0100 Subject: [PATCH 03/14] fix(v0): incorrect main return type --- tsock_v0.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsock_v0.c b/tsock_v0.c index ab70161..66a62ce 100644 --- a/tsock_v0.c +++ b/tsock_v0.c @@ -18,7 +18,7 @@ /* pour la gestion des erreurs */ #include -void main (int argc, char **argv) +int main (int argc, char **argv) { int c; extern char *optarg; From 7169fd369df39b661186334edaea42d128b4e24f Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 10:23:16 +0100 Subject: [PATCH 04/14] refactore(v3): message output --- tsock_v3.c | 62 ++++++++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/tsock_v3.c b/tsock_v3.c index ed10db1..1a9da09 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -17,9 +17,8 @@ #include /* pour la gestion des erreurs */ #include -#define BASE_SIZE 10 - +// Remplit le message de lg caractères motif void construire_message(char* message, char motif, int lg); // Affichage distinct entre envoi et reception pour une facilité d'utilisation @@ -27,8 +26,12 @@ void afficher_message_envoi(char *message, int lg, int numero_envoi); void afficher_message_reception(char *message, int lg, int numero_envoi); // Permet de compter le nombre de digits d'un nombre, utile pour l'affichage [--143 ...] +// (base 10) int count_digits(int lg); +// Affiche l'usage de l'outil +void print_usage(); + int main (int argc, char **argv) { @@ -44,7 +47,7 @@ int main (int argc, char **argv) switch (c) { case 'p': if (source == 1) { - printf("usage: cmd [-p|-s][-n ##]\n"); + print_usage(); exit(1); } source = 0; @@ -52,7 +55,7 @@ int main (int argc, char **argv) case 's': if (source == 0) { - printf("usage: cmd [-p|-s][-n ##]\n"); + print_usage(); exit(1) ; } source = 1; @@ -84,7 +87,7 @@ int main (int argc, char **argv) break; default: - printf("usage: cmd [-p|-s][-n ##]\n"); + print_usage(); break; } } @@ -100,12 +103,12 @@ int main (int argc, char **argv) lg=30; } - //Allocation du message, il est de la taille donnée en paramètre + // Allocation du message, il est de la taille donnée en paramètre char* message = malloc(lg * sizeof(char)); if (source == -1) { - printf("usage: cmd [-p|-s][-n ##]\n"); + print_usage(); exit(1) ; } @@ -133,15 +136,12 @@ int main (int argc, char **argv) } - // Communnication through UDP messages + // Communnication through UDP datagrams if (udp==1){ // Creation du socket local int sock= socket(AF_INET,SOCK_DGRAM,0); if (source==1) { - - - // Creation de l'adresse du socket distant struct hostent *hp ; struct sockaddr_in adr_dest; @@ -172,14 +172,8 @@ int main (int argc, char **argv) afficher_message_envoi(message,lg,i+1); sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest); } - - - } - - else { - - - // Creation de l'adresse du socket distant + } else { // UDP & puit + // Creation de l'adresse du socket local struct sockaddr_in adr_locale; int longueur_adr_locale = sizeof(adr_locale); @@ -205,22 +199,12 @@ int main (int argc, char **argv) // Afficher notre seule et unique triste message afficher_message_reception(message, lg, k); } - - - - } - } - - else{ - + } else { // TCP // Creation du socket local int sock= socket(AF_INET,SOCK_STREAM,0); if (source==1) { - - - // Creation de l'adresse du socket distant struct hostent *hp ; struct sockaddr_in adr_dest; @@ -261,13 +245,7 @@ int main (int argc, char **argv) // Close socket to avoid dangling connections close(sock); - - - } - - else { - - + } else { // TCP & puit // Creation de l'adresse du socket local struct sockaddr_in adr_locale; socklen_t longueur_adr_locale = sizeof(adr_locale); @@ -292,7 +270,7 @@ int main (int argc, char **argv) exit(1); } - int sock_bis; + int sock_bis; // Socket correspondant à la connexion TCP initiée par la source // Accept a single connection on the main thread if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){ printf("échec du accept\n") ; @@ -358,13 +336,11 @@ void afficher_message_reception(char *message, int lg, int numero_envoi) { } int count_digits(int lg) { - int retour; if (!lg){ retour=1; - } - else { + } else { retour=0; while (lg > 0) { lg/=10; @@ -374,3 +350,7 @@ int count_digits(int lg) { return retour; } + +void print_usage() { + printf("usage: cmd [-p|-s][-n ##]\n"); +} From fe9cd5572747c1b95a3825dc8b23482c66322e9f Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 10:23:56 +0100 Subject: [PATCH 05/14] feat(v3): parameter sanitization --- tsock_v3.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tsock_v3.c b/tsock_v3.c index 1a9da09..8c4eb5b 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -65,11 +65,13 @@ int main (int argc, char **argv) nb_message = atoi(optarg); // Packets are numerotated with 5 figures, which means the number of // packets send can't be bigger than (or equal) 10 ** 6 - if(nb_message >= 100000) { + if (nb_message >= 100000) { printf("Too many packets"); exit(1); + } else if (nb_message <= 0) { + printf("Lets try to send all at least one message (:"); + exit(1); } - break; case 'u': @@ -81,9 +83,14 @@ int main (int argc, char **argv) // A TCP packet is roughly 1500 bytes. We allow for some overhead. // We provide an MTU of 1400 bytes. if(lg >= 1400) { - printf("Messages too long"); + printf("Messages too long (max 1400)"); + exit(1); + } else if (lg < 5) { + // The message size includes the message number, whos size is 5. + printf("Messages too long (min 5)"); exit(1); } + break; default: @@ -117,7 +124,6 @@ int main (int argc, char **argv) nb_message = 10 ; } - //Affichage des informations de communication initiée if (source == 1) { //Recuperation du nom logique @@ -125,7 +131,7 @@ int main (int argc, char **argv) printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s,dest=%s\n", lg,port,nb_message,(udp)?"UDP":"TCP",nom_machine_distante); } else { - printf("PUITS:lg_mesg-lu=%d,port=%d,nb_receptions=",lg,port); + printf("PUITS:lg_mesg_lu=%d,port=%d,nb_receptions=",lg,port); if (nb_message!=-1 && !source) { printf("infini"); } From 27a98ee4b3322c887c6b89aa2fefcb88c2541dc7 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 10:47:26 +0100 Subject: [PATCH 06/14] feat(v33): descriptive usage message --- tsock_v3.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tsock_v3.c b/tsock_v3.c index 8c4eb5b..fe7cb04 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -95,7 +95,7 @@ int main (int argc, char **argv) default: print_usage(); - break; + exit(1); } } @@ -358,5 +358,12 @@ int count_digits(int lg) { } void print_usage() { - printf("usage: cmd [-p|-s][-n ##]\n"); + printf("usage: tsock_v3 [-psu] [-n nb_messages] [-l mess_length] [host] \n"); + printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); + printf(" port Port to connect or bind to. Required.\n"); + printf("options: -l mess_length Size of the messages to send. Min 5. Max 1400. Default 30. Ignored with -p.\n"); + printf(" -n nb_messages Number of messages to send. Min 1. Default 10. Ignored with -p.\n"); + printf(" -p Runs a TCP/UDP sink. Incompatible with -s.\n"); + printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n"); + printf(" -u Use UDP instead of TCP.\n"); } From a99528d35d97675d27f91bef41d7656ecefdb535 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 11:02:49 +0100 Subject: [PATCH 07/14] fix(v3): missing endlines --- tsock_v3.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tsock_v3.c b/tsock_v3.c index fe7cb04..94b1268 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -66,10 +66,10 @@ int main (int argc, char **argv) // Packets are numerotated with 5 figures, which means the number of // packets send can't be bigger than (or equal) 10 ** 6 if (nb_message >= 100000) { - printf("Too many packets"); + printf("Too many packets. (Max 99999)\n"); exit(1); } else if (nb_message <= 0) { - printf("Lets try to send all at least one message (:"); + printf("Lets try to send all at least one message (:\n"); exit(1); } break; @@ -83,11 +83,11 @@ int main (int argc, char **argv) // A TCP packet is roughly 1500 bytes. We allow for some overhead. // We provide an MTU of 1400 bytes. if(lg >= 1400) { - printf("Messages too long (max 1400)"); + printf("Messages too long (max 1400)\n"); exit(1); } else if (lg < 5) { // The message size includes the message number, whos size is 5. - printf("Messages too long (min 5)"); + printf("Messages too short (min 5)\n"); exit(1); } @@ -236,7 +236,7 @@ int main (int argc, char **argv) // Demande de connexion int succ; if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){ - printf("Echec connect"); + printf("Echec connect\n"); exit(1); } @@ -361,7 +361,7 @@ void print_usage() { printf("usage: tsock_v3 [-psu] [-n nb_messages] [-l mess_length] [host] \n"); printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); printf(" port Port to connect or bind to. Required.\n"); - printf("options: -l mess_length Size of the messages to send. Min 5. Max 1400. Default 30. Ignored with -p.\n"); + printf("options: -l mess_length Size of the messages to send. Min 5. Max 1400. Default 30.\n"); printf(" -n nb_messages Number of messages to send. Min 1. Default 10. Ignored with -p.\n"); printf(" -p Runs a TCP/UDP sink. Incompatible with -s.\n"); printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n"); From 2e156fef2fb853ef19f99fe1d17584a14369cb10 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 11:14:44 +0100 Subject: [PATCH 08/14] feat(v2): usage message --- tsock_v2.c | 88 ++++++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 53 deletions(-) diff --git a/tsock_v2.c b/tsock_v2.c index 8aaff95..0fcbc07 100644 --- a/tsock_v2.c +++ b/tsock_v2.c @@ -19,23 +19,11 @@ #include #define BASE_SIZE 10 -void construire_message(char *message, char motif, int lg) { - int i; - for (i=0;i\n"); + printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); + printf(" port Port to connect or bind to. Required.\n"); + printf("options: -p Runs a TCP/UDP sink. Incompatible with -s.\n"); + printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n"); + printf(" -u Use UDP instead of TCP.\n"); +} From 40f45b9e3bcd332740236914380587ef33f70c14 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 11:22:11 +0100 Subject: [PATCH 09/14] feat(v1): usage message --- tsock_v1.c | 72 ++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/tsock_v1.c b/tsock_v1.c index e3cce3d..65f5977 100644 --- a/tsock_v1.c +++ b/tsock_v1.c @@ -19,23 +19,12 @@ #include #define BASE_SIZE 10 -void construire_message(char *message, char motif, int lg) { - int i; - for (i=0;i [-ps] [host] \n"); + printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); + printf(" port Port to connect or bind to. Required.\n"); + printf("options: -p Runs a TCP/UDP sink. Incompatible with -s.\n"); + printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n"); + printf(" -u Use UDP instead of TCP. Required.\n"); +} + From 8bfbb170eb81e28466fcbc50e851e275988c2cf1 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 11:40:32 +0100 Subject: [PATCH 10/14] feat: command usage uses agv0 --- tsock_v1.c | 14 +++++++------- tsock_v2.c | 14 +++++++------- tsock_v3.c | 14 +++++++------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tsock_v1.c b/tsock_v1.c index 65f5977..f164b41 100644 --- a/tsock_v1.c +++ b/tsock_v1.c @@ -22,7 +22,7 @@ void construire_message(char *message, char motif, int lg); void afficher_message(char *message, int lg); -void print_usage(); +void print_usage(char* arg0); int main (int argc, char **argv) { int c; @@ -36,7 +36,7 @@ int main (int argc, char **argv) { switch (c) { case 'p': if (source == 1) { - print_usage(); + print_usage(argv[0]); exit(1); } source = 0; @@ -44,7 +44,7 @@ int main (int argc, char **argv) { case 's': if (source == 0) { - print_usage(); + print_usage(argv[0]); exit(1) ; } source = 1; @@ -59,7 +59,7 @@ int main (int argc, char **argv) { break; default: - print_usage(); + print_usage(argv[0]); exit(1); } } @@ -73,7 +73,7 @@ int main (int argc, char **argv) { char* message = malloc(BASE_SIZE * sizeof(char)); if (source == -1) { - print_usage(); + print_usage(argv[0]); exit(1) ; } @@ -179,8 +179,8 @@ void afficher_message(char *message, int lg) { printf("\n"); } -void print_usage() { - printf("usage: tsock_v1 <-u> [-ps] [host] \n"); +void print_usage(char* arg0) { + printf("usage: %s <-u> [-ps] [host] \n", arg0); printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); printf(" port Port to connect or bind to. Required.\n"); printf("options: -p Runs a TCP/UDP sink. Incompatible with -s.\n"); diff --git a/tsock_v2.c b/tsock_v2.c index 0fcbc07..bb381d8 100644 --- a/tsock_v2.c +++ b/tsock_v2.c @@ -21,7 +21,7 @@ void construire_message(char *message, char motif, int lg); void afficher_message(char *message, int lg); -void print_usage(); +void print_usage(char* arg0); int main (int argc, char **argv) { int c; @@ -35,7 +35,7 @@ int main (int argc, char **argv) { switch (c) { case 'p': if (source == 1) { - print_usage(); + print_usage(argv[0]); exit(1); } source = 0; @@ -43,7 +43,7 @@ int main (int argc, char **argv) { case 's': if (source == 0) { - print_usage(); + print_usage(argv[0]); exit(1) ; } source = 1; @@ -58,7 +58,7 @@ int main (int argc, char **argv) { break; default: - print_usage(); + print_usage(argv[0]); exit(1); } } @@ -72,7 +72,7 @@ int main (int argc, char **argv) { char* message = malloc(BASE_SIZE * sizeof(char)); if (source == -1) { - print_usage(); + print_usage(argv[0]); exit(1) ; } @@ -262,8 +262,8 @@ void afficher_message(char *message, int lg) { } -void print_usage() { - printf("usage: tsock_v2 [-psu] [host] \n"); +void print_usage(char* arg0) { + printf("usage: %s [-psu] [host] \n", arg0); printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); printf(" port Port to connect or bind to. Required.\n"); printf("options: -p Runs a TCP/UDP sink. Incompatible with -s.\n"); diff --git a/tsock_v3.c b/tsock_v3.c index 94b1268..5968d4f 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -30,7 +30,7 @@ void afficher_message_reception(char *message, int lg, int numero_envoi); int count_digits(int lg); // Affiche l'usage de l'outil -void print_usage(); +void print_usage(char* arg0); int main (int argc, char **argv) @@ -47,7 +47,7 @@ int main (int argc, char **argv) switch (c) { case 'p': if (source == 1) { - print_usage(); + print_usage(argv[0]); exit(1); } source = 0; @@ -55,7 +55,7 @@ int main (int argc, char **argv) case 's': if (source == 0) { - print_usage(); + print_usage(argv[0]); exit(1) ; } source = 1; @@ -94,7 +94,7 @@ int main (int argc, char **argv) break; default: - print_usage(); + print_usage(argv[0]); exit(1); } } @@ -115,7 +115,7 @@ int main (int argc, char **argv) if (source == -1) { - print_usage(); + print_usage(argv[0]); exit(1) ; } @@ -357,8 +357,8 @@ int count_digits(int lg) { return retour; } -void print_usage() { - printf("usage: tsock_v3 [-psu] [-n nb_messages] [-l mess_length] [host] \n"); +void print_usage(char* arg0) { + printf("usage: %s [-psu] [-n nb_messages] [-l mess_length] [host] \n", arg0); printf("parameters: host With -s, address of the host to connect to. Required with -s.\n"); printf(" port Port to connect or bind to. Required.\n"); printf("options: -l mess_length Size of the messages to send. Min 5. Max 1400. Default 30.\n"); From 586d67665d3439769bd896b9f5779970ab127955 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 11:41:02 +0100 Subject: [PATCH 11/14] doc: usage --- README.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 63ba6f7..668a8f0 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,39 @@ zip LACAU-ALNET-TP1.zip tsock_v*.c README.md ## Running -TODO help output +``` +usage: ./t_sockv1 <-u> [-ps] [host] +parameters: host With -s, address of the host to connect to. Required with -s. + port Port to connect or bind to. Required. +options: -p Runs a TCP/UDP sink. Incompatible with -s. + -s Runs a TCP/UDP faucet. Incompatible with -p. + -u Use UDP instead of TCP. Required. + +usage: ./t_sockv2 [-psu] [host] +parameters: host With -s, address of the host to connect to. Required with -s. + port Port to connect or bind to. Required. +options: -p Runs a TCP/UDP sink. Incompatible with -s. + -s Runs a TCP/UDP faucet. Incompatible with -p. + -u Use UDP instead of TCP. + +usage: ./t_sockv3 [-psu] [-n nb_messages] [-l mess_length] [host] +parameters: host With -s, address of the host to connect to. Required with -s. + port Port to connect or bind to. Required. +options: -l mess_length Size of the messages to send. Min 5. Max 1400. Default 30. + -n nb_messages Number of messages to send. Min 1. Default 10. Ignored with -p. + -p Runs a TCP/UDP sink. Incompatible with -s. + -s Runs a TCP/UDP faucet. Incompatible with -p. + -u Use UDP instead of TCP. +``` + +Example usage of v3 : + +``` +./t_sockv3 -p -l 15 4000 # receiver +./t_sockv3 -s -l 15 localhost 4000 # sender +``` + + ## Roadmap From 332d83ff0467c9ac7f72c008a48cf241cee01903 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 11:59:59 +0100 Subject: [PATCH 12/14] fix(v3): message format and count --- tsock_v3.c | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/tsock_v3.c b/tsock_v3.c index 5968d4f..5112285 100644 --- a/tsock_v3.c +++ b/tsock_v3.c @@ -19,7 +19,7 @@ #include // Remplit le message de lg caractères motif -void construire_message(char* message, char motif, int lg); +void construire_message(char *message, char motif, int lg, int numero_envoi); // Affichage distinct entre envoi et reception pour une facilité d'utilisation void afficher_message_envoi(char *message, int lg, int numero_envoi); @@ -172,7 +172,7 @@ int main (int argc, char **argv) for (int i = 0; i < nb_message; i++) { // Construction du message - construire_message(message, 'a' + (i % 26), lg); + construire_message(message, 'a' + (i % 26), lg, i+1); // Envoi du message afficher_message_envoi(message,lg,i+1); @@ -196,14 +196,14 @@ int main (int argc, char **argv) } // Suivi du numéro de message reçu - int k=0; + int k=1; while (1) { // Receive a single message because we are lazy recvfrom(sock, message, lg, 0, NULL, NULL); - k++; // Afficher notre seule et unique triste message afficher_message_reception(message, lg, k); + k++; } } } else { // TCP @@ -243,7 +243,7 @@ int main (int argc, char **argv) // Envoi des messages for (int i = 0; i < nb_message; i++) { // Construction du message - construire_message(message, 'a' + (i % 26), lg); + construire_message(message, 'a' + (i % 26), lg, i+1); afficher_message_envoi(message,lg,i+1); // Envoi du message write(sock,message,longueur_message); @@ -284,7 +284,7 @@ int main (int argc, char **argv) } // Suivi du numéro de message reçu - int k=0; + int k=1; while (read(sock_bis, message, longueur_message) > 0) { afficher_message_reception(message, longueur_message,k); @@ -299,24 +299,17 @@ int main (int argc, char **argv) } -void construire_message(char *message, char motif, int lg) { +void construire_message(char *message, char motif, int lg, int numero_envoi) { int i; - for (i=0;inb_digits; l--) { - printf("-"); - } - - printf("%d",numero_envoi); - - for (i=0;inb_digits; l--) { - printf("-"); - } - - printf("%d",numero_envoi); - - for (i=0;i Date: Thu, 7 Mar 2024 12:13:04 +0100 Subject: [PATCH 13/14] doc: add usage v4 --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 668a8f0..1a0842e 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,19 @@ Example usage of v3 : ./t_sockv3 -s -l 15 localhost 4000 # sender ``` +Example usage of v4 : +``` +./t_sockv3 -c -r -l 15 4000 # client receiver +./t_sockv3 -S -s -l 15 localhost 4000 # server sender +``` ## Roadmap - [X] tsock-v1 - [X] tsock-v2 -- [WIP] tsock-v3 -- [ ] tsock-v4 +- [X] tsock-v3 +- [WIP] tsock-v4 + - [X] server can handle multiple requests simultaneously + - [X] server doesn't stop after the connection ends + - [X] both client and server can assume sender/receiver roles From aec652d6926e6a1033cb444c1577d0c5ddb2b5a3 Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Thu, 7 Mar 2024 12:15:06 +0100 Subject: [PATCH 14/14] doc: add usage v4 again --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1a0842e..5ccaca1 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,8 @@ Example usage of v3 : Example usage of v4 : ``` -./t_sockv3 -c -r -l 15 4000 # client receiver -./t_sockv3 -S -s -l 15 localhost 4000 # server sender +./t_sockv3 -C -e -n 20 -l 15 localhost 4000 # client sender +./t_sockv3 -S -r -n 20 -l 15 4000 # server receiver ``` ## Roadmap @@ -75,7 +75,7 @@ Example usage of v4 : - [X] tsock-v1 - [X] tsock-v2 - [X] tsock-v3 -- [WIP] tsock-v4 +- [X] tsock-v4 - [X] server can handle multiple requests simultaneously - [X] server doesn't stop after the connection ends - [X] both client and server can assume sender/receiver roles