Merge branch 'refactore'
This commit is contained in:
commit
87a6b29664
5 changed files with 771 additions and 766 deletions
57
README.md
57
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) :
|
||||
|
@ -22,11 +31,51 @@ zip LACAU-ALNET-TP1.zip tsock_v*.c README.md
|
|||
|
||||
## Running
|
||||
|
||||
TODO help output
|
||||
```
|
||||
usage: ./t_sockv1 <-u> [-ps] [host] <port>
|
||||
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] <port>
|
||||
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] <port>
|
||||
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
|
||||
```
|
||||
|
||||
Example usage of v4 :
|
||||
|
||||
```
|
||||
./t_sockv3 -C -e -n 20 -l 15 localhost 4000 # client sender
|
||||
./t_sockv3 -S -r -n 20 -l 15 4000 # server receiver
|
||||
```
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [X] tsock-v1
|
||||
- [X] tsock-v2
|
||||
- [WIP] tsock-v3
|
||||
- [ ] tsock-v4
|
||||
- [X] tsock-v3
|
||||
- [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
|
||||
|
|
|
@ -18,7 +18,7 @@ données du réseau */
|
|||
/* pour la gestion des erreurs */
|
||||
#include <errno.h>
|
||||
|
||||
void main (int argc, char **argv)
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
int c;
|
||||
extern char *optarg;
|
||||
|
|
72
tsock_v1.c
72
tsock_v1.c
|
@ -19,23 +19,12 @@ données du réseau */
|
|||
#include <errno.h>
|
||||
#define BASE_SIZE 10
|
||||
|
||||
void construire_message(char *message, char motif, int lg) {
|
||||
int i;
|
||||
for (i=0;i<lg;i++) message[i] = motif;
|
||||
}
|
||||
|
||||
void afficher_message(char *message, int lg) {
|
||||
int i;
|
||||
printf("message reçu : ");
|
||||
for (i=0;i<lg;i++){
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
void construire_message(char *message, char motif, int lg);
|
||||
void afficher_message(char *message, int lg);
|
||||
void print_usage(char* arg0);
|
||||
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
int main (int argc, char **argv) {
|
||||
int c;
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
|
@ -47,7 +36,7 @@ int main (int argc, char **argv)
|
|||
switch (c) {
|
||||
case 'p':
|
||||
if (source == 1) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
source = 0;
|
||||
|
@ -55,7 +44,7 @@ int main (int argc, char **argv)
|
|||
|
||||
case 's':
|
||||
if (source == 0) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1) ;
|
||||
}
|
||||
source = 1;
|
||||
|
@ -70,8 +59,8 @@ int main (int argc, char **argv)
|
|||
break;
|
||||
|
||||
default:
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
break;
|
||||
print_usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,7 +73,7 @@ int main (int argc, char **argv)
|
|||
char* message = malloc(BASE_SIZE * sizeof(char));
|
||||
|
||||
if (source == -1) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
|
@ -93,9 +82,7 @@ int main (int argc, char **argv)
|
|||
|
||||
//Recuperation du nom logique
|
||||
nom_machine_distante=argv[argc-2];
|
||||
}
|
||||
|
||||
else
|
||||
} else
|
||||
printf("on est dans le puits\n");
|
||||
|
||||
if (nb_message != -1) {
|
||||
|
@ -109,7 +96,6 @@ int main (int argc, char **argv)
|
|||
printf("nb de tampons à envoyer = 10 par défaut\n");
|
||||
} else
|
||||
printf("nb de tampons à envoyer = infini\n");
|
||||
|
||||
}
|
||||
|
||||
if (udp==1){
|
||||
|
@ -117,9 +103,6 @@ int main (int argc, char **argv)
|
|||
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;
|
||||
|
@ -150,14 +133,8 @@ int main (int argc, char **argv)
|
|||
|
||||
sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
|
||||
} else { // UDP & Puit
|
||||
// Creation de l'adresse du socket distant
|
||||
struct sockaddr_in adr_locale;
|
||||
int longueur_adr_locale = sizeof(adr_locale);
|
||||
|
@ -183,12 +160,31 @@ int main (int argc, char **argv)
|
|||
// Afficher notre seule et unique triste message
|
||||
afficher_message(message, longueur_message);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void construire_message(char *message, char motif, int lg) {
|
||||
int i;
|
||||
for (i=0;i<lg;i++) message[i] = motif;
|
||||
}
|
||||
|
||||
void afficher_message(char *message, int lg) {
|
||||
int i;
|
||||
printf("message reçu : ");
|
||||
for (i=0;i<lg;i++){
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void print_usage(char* arg0) {
|
||||
printf("usage: %s <-u> [-ps] [host] <port>\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");
|
||||
printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n");
|
||||
printf(" -u Use UDP instead of TCP. Required.\n");
|
||||
}
|
||||
|
||||
|
|
88
tsock_v2.c
88
tsock_v2.c
|
@ -19,23 +19,11 @@ données du réseau */
|
|||
#include <errno.h>
|
||||
#define BASE_SIZE 10
|
||||
|
||||
void construire_message(char *message, char motif, int lg) {
|
||||
int i;
|
||||
for (i=0;i<lg;i++) message[i] = motif;
|
||||
}
|
||||
void construire_message(char *message, char motif, int lg);
|
||||
void afficher_message(char *message, int lg);
|
||||
void print_usage(char* arg0);
|
||||
|
||||
void afficher_message(char *message, int lg) {
|
||||
int i;
|
||||
printf("message reçu : ");
|
||||
for (i=0;i<lg;i++){
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
int main (int argc, char **argv) {
|
||||
int c;
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
|
@ -47,7 +35,7 @@ int main (int argc, char **argv)
|
|||
switch (c) {
|
||||
case 'p':
|
||||
if (source == 1) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
source = 0;
|
||||
|
@ -55,7 +43,7 @@ int main (int argc, char **argv)
|
|||
|
||||
case 's':
|
||||
if (source == 0) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1) ;
|
||||
}
|
||||
source = 1;
|
||||
|
@ -70,8 +58,8 @@ int main (int argc, char **argv)
|
|||
break;
|
||||
|
||||
default:
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
break;
|
||||
print_usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,7 +72,7 @@ int main (int argc, char **argv)
|
|||
char* message = malloc(BASE_SIZE * sizeof(char));
|
||||
|
||||
if (source == -1) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
|
@ -94,7 +82,6 @@ int main (int argc, char **argv)
|
|||
} else printf("PUITS:");
|
||||
|
||||
|
||||
|
||||
if (nb_message != -1) {
|
||||
if (source == 1)
|
||||
printf("nb de tampons à envoyer : %d\n", nb_message);
|
||||
|
@ -106,7 +93,6 @@ int main (int argc, char **argv)
|
|||
printf("nb de tampons à envoyer = 10 par défaut\n");
|
||||
} else
|
||||
printf("nb de tampons à envoyer = infini\n");
|
||||
|
||||
}
|
||||
|
||||
if (udp==1){
|
||||
|
@ -147,13 +133,7 @@ int main (int argc, char **argv)
|
|||
|
||||
sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
|
||||
} else { // UDP & puit
|
||||
// Creation de l'adresse du socket distant
|
||||
struct sockaddr_in adr_locale;
|
||||
int longueur_adr_locale = sizeof(adr_locale);
|
||||
|
@ -179,22 +159,12 @@ int main (int argc, char **argv)
|
|||
// Afficher notre seule et unique triste message
|
||||
afficher_message(message, longueur_message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -235,13 +205,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);
|
||||
|
@ -278,13 +242,31 @@ int main (int argc, char **argv)
|
|||
}
|
||||
close(sock_bis);
|
||||
close(sock);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void construire_message(char *message, char motif, int lg) {
|
||||
int i;
|
||||
for (i=0;i<lg;i++) message[i] = motif;
|
||||
}
|
||||
|
||||
void afficher_message(char *message, int lg) {
|
||||
int i;
|
||||
printf("message reçu : ");
|
||||
for (i=0;i<lg;i++){
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
void print_usage(char* arg0) {
|
||||
printf("usage: %s [-psu] [host] <port>\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");
|
||||
printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n");
|
||||
printf(" -u Use UDP instead of TCP.\n");
|
||||
}
|
||||
|
|
122
tsock_v3.c
122
tsock_v3.c
|
@ -17,18 +17,21 @@ données du réseau */
|
|||
#include <stdio.h>
|
||||
/* pour la gestion des erreurs */
|
||||
#include <errno.h>
|
||||
#define BASE_SIZE 10
|
||||
|
||||
|
||||
void construire_message(char* message, char motif, int lg);
|
||||
// Remplit le message de lg caractères motif
|
||||
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);
|
||||
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(char* arg0);
|
||||
|
||||
|
||||
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(argv[0]);
|
||||
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(argv[0]);
|
||||
exit(1) ;
|
||||
}
|
||||
source = 1;
|
||||
|
@ -63,10 +66,12 @@ 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 (:\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
|
@ -78,14 +83,19 @@ 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)\n");
|
||||
exit(1);
|
||||
} else if (lg < 5) {
|
||||
// The message size includes the message number, whos size is 5.
|
||||
printf("Messages too short (min 5)\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
break;
|
||||
print_usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,7 +115,7 @@ int main (int argc, char **argv)
|
|||
|
||||
|
||||
if (source == -1) {
|
||||
printf("usage: cmd [-p|-s][-n ##]\n");
|
||||
print_usage(argv[0]);
|
||||
exit(1) ;
|
||||
}
|
||||
|
||||
|
@ -114,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
|
||||
|
@ -122,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");
|
||||
}
|
||||
|
@ -133,15 +142,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;
|
||||
|
@ -166,20 +172,14 @@ 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);
|
||||
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);
|
||||
|
||||
|
@ -196,31 +196,21 @@ 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{
|
||||
|
||||
} 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;
|
||||
|
@ -246,14 +236,14 @@ 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
@ -261,13 +251,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 +276,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") ;
|
||||
|
@ -300,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);
|
||||
|
@ -315,23 +299,16 @@ 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;i<lg;i++) message[i] = motif;
|
||||
snprintf(message, lg,"% 5d",numero_envoi);
|
||||
for (i=5;i<lg;i++) message[i] = motif;
|
||||
}
|
||||
|
||||
void afficher_message_envoi(char *message, int lg, int numero_envoi) {
|
||||
int i;
|
||||
printf("SOURCE: Envoi n°%d (%d) [",numero_envoi,lg);
|
||||
|
||||
int nb_digits = count_digits(numero_envoi);
|
||||
|
||||
for (int l=5; l>nb_digits; l--) {
|
||||
printf("-");
|
||||
}
|
||||
|
||||
printf("%d",numero_envoi);
|
||||
|
||||
for (i=0;i<lg;i++) {
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
|
@ -342,29 +319,19 @@ void afficher_message_reception(char *message, int lg, int numero_envoi) {
|
|||
int i;
|
||||
printf("PUITS: Reception n°%d (%d) [",numero_envoi,lg);
|
||||
|
||||
int nb_digits = count_digits(numero_envoi);
|
||||
|
||||
for (int l=5; l>nb_digits; l--) {
|
||||
printf("-");
|
||||
}
|
||||
|
||||
printf("%d",numero_envoi);
|
||||
|
||||
for (i=0;i<lg;i++) {
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
printf("]\n");
|
||||
|
||||
}
|
||||
|
||||
int count_digits(int lg) {
|
||||
|
||||
int count_digits(int lg) {
|
||||
int retour;
|
||||
|
||||
if (!lg){
|
||||
retour=1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
retour=0;
|
||||
while (lg > 0) {
|
||||
lg/=10;
|
||||
|
@ -374,3 +341,14 @@ int count_digits(int lg) {
|
|||
|
||||
return retour;
|
||||
}
|
||||
|
||||
void print_usage(char* arg0) {
|
||||
printf("usage: %s [-psu] [-n nb_messages] [-l mess_length] [host] <port>\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");
|
||||
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");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue