port_affichage enlevé

This commit is contained in:
nbillard 2023-02-13 09:03:13 +01:00
parent 41fd577b34
commit b00322effb

View file

@ -25,7 +25,7 @@ void afficher_message(char *message, int lg, int num) {
} }
void usage() { void usage() {
printf("usage: cmd [-p|-s][-u|-t] [-n ##] [-l ##]\n"); printf("usage: cmd [-p|-s] [-c] [-e|-r] [-u|-t] [-n ##] [-l ##]\n");
} }
@ -37,6 +37,8 @@ int main (int argc, char **argv) {
int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ 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 source = -1 ; /* 0=puits, 1=source */
int transport = -1 ; /* 0=UDP, 1=TCP <- par défaut*/ int transport = -1 ; /* 0=UDP, 1=TCP <- par défaut*/
int client = 0; /* 0=client, 1=serveur */
int recepteur = -1; /* 0=éméteur, 1=recepteur (pour un client) */
struct hostent *hp; struct hostent *hp;
struct sockaddr_in adr_em; struct sockaddr_in adr_em;
@ -55,55 +57,81 @@ int main (int argc, char **argv) {
int nb_max_connexion = 5; int nb_max_connexion = 5;
char* pmesg; char* pmesg;
while ((c = getopt(argc, argv, "pstun:l:")) != -1) { while ((c = getopt(argc, argv, "pscertun:l:")) != -1) {
switch (c) { switch (c) {
case 'p': /* On choisit le puit. */ case 'c': /* Si on est en mode tcp, on devient client */
if (source == 1) { if (transport && ( source == 1 || source == 0) ) { /* on ne peut pas être client et serveur à la fois */
usage();
exit(1);
}
client = 1;
break;
case 'e': /* Client emetteur */
if (recepteur == 1 || client != 1) {
usage();
exit(1);
}
recepteur = 0;
break;
case 'r': /* Client recepteur */
if (recepteur == 0 || client != 1) {
usage();
exit(1);
}
recepteur = 1;
break;
case 'p': /* On choisit le puit. */
if (source == 1) {
usage();
exit(1);
}
source = 0;
break;
case 's': /* On choisit la source. */
if (source == 0) {
usage();
exit(1) ;
}
source = 1;
break;
case 'u': /* On choisit le mode UDP. */
if (transport == 1) {
usage();
exit(1);
}
transport = 0;
break;
case 't': /* On choisit le mode TCP */
if (transport == 0) {
usage();
exit(1);
}
transport = 1;
break;
case 'n': /* On choisit le nombre de messages à envoyer ou à lire. */
nb_message = atoi(optarg);
break;
case 'l': /* On choisit la longueur des messages à envoyer ou la longueur maximale des messages à lire. */
lg_mesg = atoi(optarg);
break;
default:
usage(); usage();
exit(1); break;
}
source = 0;
break;
case 's': /* On choisit la source. */
if (source == 0) {
usage();
exit(1) ;
}
source = 1;
break;
case 'u': /* On choisit le mode UDP. */
if (transport == 1) {
usage();
exit(1);
}
transport = 0;
break;
case 't': /* On choisit le mode TCP */
if (transport == 0) {
usage();
exit(1);
}
transport = 1;
break;
case 'n': /* On choisit le nombre de messages à envoyer ou à lire. */
nb_message = atoi(optarg);
break;
case 'l': /* On choisit la longueur des messages à envoyer ou la longueur maximale des messages à lire. */
lg_mesg = atoi(optarg);
break;
default:
usage();
break;
} }
} }
if (transport == -1) { if (client == 1 && recepteur == -1) { /* Si on est client on doit préciser si on est recepteur ou emetteur */
usage();
exit(1);
}
if (transport == -1) { /* Protocole par défault est TCP */
transport = 1; transport = 1;
} }
@ -112,7 +140,7 @@ int main (int argc, char **argv) {
exit(1) ; exit(1) ;
} }
if(argc==optind){ if(argc==optind){ /* Il faut préciser le port de la machine vers laquelle on veut se connecter */
exit(1); exit(1);
} }
@ -131,11 +159,10 @@ int main (int argc, char **argv) {
} }
int port_affichage = port ;
port = htons(port); port = htons(port);
pmesg = malloc(sizeof(char)*lg_mesg); pmesg = malloc(sizeof(char)*lg_mesg);
if (port_affichage<5000){ if (port<5000){
printf("Le numéro de port doit être supérieur à 5000.\n"); printf("Le numéro de port doit être supérieur à 5000.\n");
exit(1); exit(1);
} }
@ -150,7 +177,7 @@ int main (int argc, char **argv) {
if (nb_message == -1) { if (nb_message == -1) {
nb_message = 10; // par défaut nb_message = 10; // par défaut
} }
printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=udp, dest=%s\n", lg_mesg, port_affichage, nb_message, argv[argc-2]); printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=udp, dest=%s\n", lg_mesg, port, nb_message, argv[argc-2]);
//création du socket local //création du socket local
if((sock=socket(AF_INET, SOCK_DGRAM,0)) == -1) { if((sock=socket(AF_INET, SOCK_DGRAM,0)) == -1) {
@ -198,9 +225,9 @@ int main (int argc, char **argv) {
else { else {
if (nb_message == -1) { if (nb_message == -1) {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=infini, TP=udp\n", lg_mesg, port_affichage); printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=infini, TP=udp\n", lg_mesg, port);
} else { } else {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=%d, TP=udp\n", lg_mesg, port_affichage, nb_message); printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=%d, TP=udp\n", lg_mesg, port, nb_message);
} }
//création du socket //création du socket
@ -259,7 +286,7 @@ int main (int argc, char **argv) {
nb_message = 10; nb_message = 10;
} }
printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=tcp, dest=%s\n", lg_mesg, port_affichage, nb_message, argv[argc-2]); printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=tcp, dest=%s\n", lg_mesg, port, nb_message, argv[argc-2]);
//création du socket local //création du socket local
if((sock=socket(AF_INET, SOCK_STREAM,0)) == -1) { if((sock=socket(AF_INET, SOCK_STREAM,0)) == -1) {
@ -317,9 +344,9 @@ int main (int argc, char **argv) {
else { else {
if (nb_message == -1) { if (nb_message == -1) {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=infini, TP=tcp\n", lg_mesg, port_affichage); printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=infini, TP=tcp\n", lg_mesg, port);
} else { } else {
printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=%d, TP=tcp\n", lg_mesg, port_affichage, nb_message); printf("PUITS : lg_mesg_lu=%d, port=%d, nb_receptions=%d, TP=tcp\n", lg_mesg, port, nb_message);
} }