(tsock_v4) lots of minor modifications
This commit is contained in:
parent
6b634e221a
commit
cb70cc4e8c
3 changed files with 68 additions and 48 deletions
|
@ -66,8 +66,12 @@ Example usage of v3 :
|
||||||
Example usage of v4 :
|
Example usage of v4 :
|
||||||
|
|
||||||
```
|
```
|
||||||
./t_sockv3 -C -e -n 20 -l 15 localhost 4000 # client sender
|
./t_sockv4 -S -r -n 20 -l 15 4000 # server receiver
|
||||||
./t_sockv3 -S -r -n 20 -l 15 4000 # server receiver
|
./t_sockv4 -C -e -n 20 -l 15 localhost 4000 # client sender
|
||||||
|
|
||||||
|
./t_sockv4 -S -e -n 20 -l 15 4000 # server sender
|
||||||
|
./t_sockv4 -C -r -n 20 -l 15 localhost 4000 # client receiver
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
|
@ -26,7 +26,7 @@ void afficher_message_envoi(char *message, int lg, int numero_envoi);
|
||||||
void afficher_message_reception(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 ...]
|
// Permet de compter le nombre de digits d'un nombre, utile pour l'affichage [--143 ...]
|
||||||
// (base 10)
|
// (base 10). Première solution d'affichage mais nous nous servons finalement de snprintf().
|
||||||
int count_digits(int lg);
|
int count_digits(int lg);
|
||||||
|
|
||||||
// Affiche l'usage de l'outil
|
// Affiche l'usage de l'outil
|
||||||
|
|
106
tsock_v4.c
106
tsock_v4.c
|
@ -17,14 +17,17 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
/* pour la gestion des erreurs */
|
/* pour la gestion des erreurs */
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#define QUEUE_SIZE 5
|
#define QUEUE_SIZE 5 // Taille de la file d'attente du listen()
|
||||||
|
|
||||||
|
// Remplit le message de lg caractères motif
|
||||||
void construire_message(char* message, char motif, int lg, int numero_envoi);
|
void construire_message(char* message, char motif, int lg, int numero_envoi);
|
||||||
|
|
||||||
// Affichage distinct entre envoi et reception pour une facilité d'utilisation
|
// Affichage distinct entre envoi et reception pour une facilité d'utilisation
|
||||||
void afficher_message_envoi(char *message, int lg, int numero_envoi);
|
void afficher_message_envoi(char *message, int lg, int numero_envoi);
|
||||||
void afficher_message_reception(char *message, int lg, int numero_envoi);
|
void afficher_message_reception(char *message, int lg, int numero_envoi);
|
||||||
|
|
||||||
|
// Affiche l'usage de l'outil
|
||||||
|
void print_usage(char* arg0);
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
@ -44,7 +47,7 @@ int main (int argc, char **argv)
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'p':
|
case 'p':
|
||||||
if (source == 1) {
|
if (source == 1) {
|
||||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
print_usage(argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
source = 0;
|
source = 0;
|
||||||
|
@ -52,7 +55,7 @@ int main (int argc, char **argv)
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
if (source == 0) {
|
if (source == 0) {
|
||||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
print_usage(argv[0]);
|
||||||
exit(1) ;
|
exit(1) ;
|
||||||
}
|
}
|
||||||
source = 1;
|
source = 1;
|
||||||
|
@ -62,11 +65,10 @@ int main (int argc, char **argv)
|
||||||
nb_message = atoi(optarg);
|
nb_message = atoi(optarg);
|
||||||
// Packets are numerotated with 5 figures, which means the number of
|
// Packets are numerotated with 5 figures, which means the number of
|
||||||
// packets send can't be bigger than (or equal) 10 ** 6
|
// packets send can't be bigger than (or equal) 10 ** 6
|
||||||
if(nb_message >= 100000) {
|
if(nb_message >= 100000 || nb_message <= 0) {
|
||||||
printf("Too many packets");
|
print_usage(argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
|
@ -79,77 +81,85 @@ int main (int argc, char **argv)
|
||||||
// We provide an MTU of 1400 bytes.
|
// We provide an MTU of 1400 bytes.
|
||||||
// We also to send at least a message of 5 characters length for indice packet
|
// We also to send at least a message of 5 characters length for indice packet
|
||||||
if(lg >= 1400 || lg < 5) {
|
if(lg >= 1400 || lg < 5) {
|
||||||
printf("Messages too long");
|
print_usage(argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/* -c and -e options stand only for TCP exchange */
|
/* -c and -e options stand only for TCP exchange */
|
||||||
case 'C':
|
case 'C':
|
||||||
client=1;
|
client=1;
|
||||||
if (udp == 1){
|
if (udp || serveur) {
|
||||||
printf("No option -C for UDP");
|
print_usage(argv[0]);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
serveur = 1;
|
serveur = 1;
|
||||||
if (udp == 1){
|
if (udp || client) {
|
||||||
printf("No option -S for UDP");
|
print_usage(argv[0]);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
emetteur=1;
|
emetteur=1;
|
||||||
if (udp == 1 || recepteur == 1){
|
if (udp || recepteur) {
|
||||||
printf("Missuse of -e option");
|
print_usage(argv[0]);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
recepteur=1;
|
recepteur=1;
|
||||||
if (udp == 1 || emetteur == 1){
|
if (udp || emetteur) {
|
||||||
printf("Missuse of -r option");
|
print_usage(argv[0]);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
print_usage(argv[0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!recepteur && !emetteur && !udp) {
|
// Little verification of TCP arguments.
|
||||||
printf("Need to specify sender or receiver for TCP connexion");
|
// Machine must be a server or a client as once,
|
||||||
exit(0);
|
// as well as a sender or receiver.
|
||||||
|
if (!udp) {
|
||||||
|
if ((!recepteur && !emetteur) || (!serveur && !client)) {
|
||||||
|
print_usage(argv[0]);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Variable qui va stocker le nom logique de la machine avec qui on communique
|
||||||
char* nom_machine_distante;
|
char* nom_machine_distante;
|
||||||
|
|
||||||
|
//Allocation de la variable stockant un message, il est de la taille donnée en paramètre
|
||||||
|
char* message = malloc(lg * sizeof(char));
|
||||||
|
|
||||||
// Recuperation du port
|
// Recuperation du port
|
||||||
int port=atoi(argv[argc-1]);
|
int port=atoi(argv[argc-1]);
|
||||||
//port = htons(port);
|
|
||||||
|
|
||||||
// Default set of packet length : 30 bytes
|
// Default set of packet length : 30 bytes
|
||||||
if (lg == -1) {
|
if (lg == -1) {
|
||||||
lg=30;
|
lg=30;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Allocation du message, il est de la taille donnée en paramètre
|
// If number of messages is not fixed for the source, it is set to 10 messages
|
||||||
char* message = malloc(lg * sizeof(char));
|
if (nb_message == -1 && (source || !udp)) {
|
||||||
|
|
||||||
|
|
||||||
// If number of messages is not fixed for the source, it is set to 10 messages
|
|
||||||
if (nb_message == -1 && source ) {
|
|
||||||
nb_message = 10 ;
|
nb_message = 10 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Affichage des informations de communication initiée
|
//Affichage des informations de communication initiée
|
||||||
if (source || client) {
|
if (source || emetteur) {
|
||||||
//Recuperation du nom logique
|
//Recuperation du nom logique
|
||||||
nom_machine_distante=argv[argc-2];
|
nom_machine_distante=argv[argc-2];
|
||||||
printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s,dest=%s\n",
|
printf("SOURCE:lg_mesg_emis=%d,port=%d,nb_envois=%d,TP=%s",
|
||||||
lg,port,nb_message,(udp)?"UDP":"TCP",nom_machine_distante);
|
lg,port,nb_message,(udp)?"UDP":"TCP");
|
||||||
|
if (client) {
|
||||||
|
printf(",dest=%s",nom_machine_distante);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
} else {
|
} 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) {
|
if (nb_message!=-1 && !source) {
|
||||||
|
@ -278,7 +288,7 @@ int main (int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Envoi des messages
|
// Envoi ou reception des messages
|
||||||
for (int i = 0; i < nb_message; i++) {
|
for (int i = 0; i < nb_message; i++) {
|
||||||
|
|
||||||
if (emetteur){
|
if (emetteur){
|
||||||
|
@ -296,19 +306,12 @@ int main (int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Close socket to avoid dangling connections
|
// Close socket to avoid dangling connections
|
||||||
close(sock);
|
close(sock);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The server shall accept the connexion
|
// The server shall accept the connexion
|
||||||
if (serveur) {
|
if (serveur) {
|
||||||
|
|
||||||
|
|
||||||
// Creation de l'adresse du socket local
|
// Creation de l'adresse du socket local
|
||||||
struct sockaddr_in adr_locale;
|
struct sockaddr_in adr_locale;
|
||||||
socklen_t longueur_adr_locale = sizeof(adr_locale);
|
socklen_t longueur_adr_locale = sizeof(adr_locale);
|
||||||
|
@ -327,7 +330,7 @@ int main (int argc, char **argv)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set listening queue size
|
// Set listening queue size
|
||||||
if (listen(sock, QUEUE_SIZE) == -1) {
|
if (listen(sock, QUEUE_SIZE) == -1) {
|
||||||
printf("échec et mat listen\n");
|
printf("échec et mat listen\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -341,17 +344,17 @@ int main (int argc, char **argv)
|
||||||
printf("échec du accept\n") ;
|
printf("échec du accept\n") ;
|
||||||
exit(1) ;
|
exit(1) ;
|
||||||
}
|
}
|
||||||
printf("socket accepte:%d\n",sock_bis);
|
|
||||||
|
|
||||||
|
|
||||||
|
// On crée un processus pour chaque connexion TCP acceptée
|
||||||
switch (fork() ) {
|
switch (fork() ) {
|
||||||
|
|
||||||
case - 1 : /* il y a une erreur */
|
case - 1 : /* il y a une erreur */
|
||||||
printf("erreur fork\n") ; exit(1) ;
|
printf("erreur fork\n") ; exit(1) ;
|
||||||
|
|
||||||
case 0 : /* on est dans le proc. fils */
|
case 0 : /* on est dans le proc. fils */
|
||||||
close(sock) ; /* fermeture socket du proc. père */
|
close(sock) ; /* fermeture socket du proc. père */
|
||||||
for (int i=0 ; i < nb_message ; i ++) {
|
for (int i=0 ; i < nb_message ; i ++) {
|
||||||
|
// Server sends packets with -e
|
||||||
// Server send packets
|
|
||||||
if (emetteur){
|
if (emetteur){
|
||||||
// Construction du message
|
// Construction du message
|
||||||
construire_message(message, 'a' + (i % 26), lg, i+1);
|
construire_message(message, 'a' + (i % 26), lg, i+1);
|
||||||
|
@ -359,7 +362,7 @@ int main (int argc, char **argv)
|
||||||
// Envoi du message
|
// Envoi du message
|
||||||
write(sock_bis,message,longueur_message);
|
write(sock_bis,message,longueur_message);
|
||||||
}
|
}
|
||||||
// Server receives packets
|
// Server receives packets with -r
|
||||||
if (recepteur) {
|
if (recepteur) {
|
||||||
if ((read(sock_bis, message, longueur_message)) < 0){
|
if ((read(sock_bis, message, longueur_message)) < 0){
|
||||||
printf("échec du read\n") ; exit(1) ;}
|
printf("échec du read\n") ; exit(1) ;}
|
||||||
|
@ -406,4 +409,17 @@ void afficher_message_reception(char *message, int lg, int numero_envoi) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_usage(char* arg0) {
|
||||||
|
printf("usage: %s [-psuCSer] [-n nb_messages] [-l mess_length] [host] <port>\n", arg0);
|
||||||
|
printf("parameters: host With -s or -C, address of the host to connect to. Required with -s or -C.\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");
|
||||||
|
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(" -C Client of a TCP connexion. Incompatible with -S and -u. Requires -e or -r.\n");
|
||||||
|
printf(" -S Server of a TCP connexion. Incompatible with -C and -u. Requires -e or -r.\n");
|
||||||
|
printf(" -e Packets issuer of a TCP connexion. Incompatible with -r and -u.\n");
|
||||||
|
printf(" -r Packets receiver of a TCP connexion. Incompatible with -e and -u.\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue