diff --git a/header/puit.h b/header/puit.h index d7b13fd..f6c9749 100644 --- a/header/puit.h +++ b/header/puit.h @@ -3,6 +3,5 @@ #include "tsock.h" int launchPuit(int nombreMessage,int tailleMessage,int isTCP); -int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct); -int exitMax(int var,int tailleMax); +int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct, int isTCP); #endif \ No newline at end of file diff --git a/header/source.h b/header/source.h index f04058d..2b6fab6 100644 --- a/header/source.h +++ b/header/source.h @@ -3,6 +3,6 @@ #include "tsock.h" int launchSource(int nombreMessage,int tailleMessage,int isTCP); -int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct); +int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct, int isTCP); #endif \ No newline at end of file diff --git a/header/tsock.h b/header/tsock.h index 59708e0..609cd3e 100644 --- a/header/tsock.h +++ b/header/tsock.h @@ -1,21 +1,23 @@ #ifndef HEADER_TSOCK #define HEADER_TSOCK +#include #include +#include #include #include #include #include #include #include -#include #include -#include void setNbMessage(int * nb, int source); void printInfo(int nb, int source, int portNumber, char ipAddress[]); void initStructSocket(struct sockaddr_in *socketServerTemp, int source); void getNonOtpArgs(char ** argv, int argc, int portNumber, char * ipAddress[]); +void formatText(char * actualMessage, int num, int tailleMessage, char messageChar); +int exitMax(int var,int tailleMax); #define PORT_NUM 9000 diff --git a/src/main.c b/src/main.c index 51c92aa..6e5186d 100644 --- a/src/main.c +++ b/src/main.c @@ -8,7 +8,7 @@ int main (int argc, char **argv) char *ipAddress; extern char *optarg; extern int optind; - int source = -1, nb_message = -1, c, tcp=1, port=9000, tailleMessage; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ + int source = -1, nb_message = -1, c, tcp=1, port=9000, tailleMessage=-1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ while ((c = getopt(argc, argv, "pn:sul:")) != -1) { switch (c) { case 'p': @@ -45,6 +45,10 @@ int main (int argc, char **argv) printf(usageChar); exit(1); } + if(tailleMessage == -1) + { + tailleMessage = 30; + } getNonOtpArgs(argv, argc, port, &ipAddress); setNbMessage(&nb_message,source); diff --git a/src/puit.c b/src/puit.c index 69e3e0e..c631d0d 100644 --- a/src/puit.c +++ b/src/puit.c @@ -4,14 +4,7 @@ int launchPuit(int nombreMessage,int tailleMessage,int isTCP) { int sock,socketType; struct sockaddr_in socketPuit; - if(isTCP) - { - socketType=SOCK_STREAM; - } - else - { - socketType=SOCK_DGRAM; - } + socketType = (isTCP) ? SOCK_STREAM : SOCK_DGRAM; if((sock=socket(AF_INET,socketType,0)) == -1) { @@ -24,55 +17,52 @@ int launchPuit(int nombreMessage,int tailleMessage,int isTCP) perror("[tsock] : fonction bind() : echec du lien avec socket serveur.\n"); exit(EXIT_FAILURE); } - if(isTCP) - { - unsigned int longueurRecu = sizeof(socketPuit); - char messageRecu[30]; - listen(sock,10); - int acceptValue = accept(sock,(struct sockaddr *)&socketPuit,&longueurRecu); - printf("AcceptValue = %d\n",acceptValue); - int tailleMessageRecu = read(acceptValue,messageRecu,sizeof(messageRecu)); - messageRecu[tailleMessageRecu]='\0'; - printf("Message TCP [%d] : %s\n",tailleMessageRecu,messageRecu); - } - else - { - receiveMultipleData(nombreMessage,tailleMessage,sock,socketPuit); - } + receiveMultipleData(nombreMessage,tailleMessage,sock,socketPuit,isTCP); close(sock); return 0; } -int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct) +int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct, int isTCP) { char messageRecu[tailleMessage+1]; - int n, longueurRecu = sizeof(socketStruct); + int n=1, longueurRecu = sizeof(socketStruct); + if(isTCP) + { + listen(sock,5); + sock = accept(sock,(struct sockaddr *)&socketStruct,(socklen_t * restrict)&longueurRecu); + } if(nombreMessages < 0) { int i=1; - while(1) + while(n>0) { - n = recvfrom(sock, (char *)messageRecu, tailleMessage, 0, (struct sockaddr*) &socketStruct,(socklen_t *__restrict__)&longueurRecu); + if(isTCP) + { + n = read(sock,messageRecu,tailleMessage); + } + else + { + n = recvfrom(sock, (char *)messageRecu, tailleMessage, 0, (struct sockaddr*) &socketStruct,(socklen_t *__restrict__)&longueurRecu); + } messageRecu[n] = '\0'; - printf("Puit\tReception n°%d (%d) :\t[%s]\n",i,tailleMessage,messageRecu); + printf("Puit\tReception n°%d (%d) :\t[%s]\n",i,n,messageRecu); i++; } } else{ for(int i=0;i1500){ - printf("-l doit être <1500 \n"); - exit(EXIT_FAILURE); - } - return 0; -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/source.c b/src/source.c index 643c335..d1dc4d6 100644 --- a/src/source.c +++ b/src/source.c @@ -4,14 +4,7 @@ int launchSource(int nombreMessage,int tailleMessage,int isTCP) { int sock,socketType; struct sockaddr_in socketSource; - if(isTCP) - { - socketType=SOCK_STREAM; - } - else - { - socketType=SOCK_DGRAM; - } + socketType = (isTCP) ? SOCK_STREAM : SOCK_DGRAM; if((sock=socket(AF_INET,socketType,0)) == -1) { @@ -22,30 +15,35 @@ int launchSource(int nombreMessage,int tailleMessage,int isTCP) if(isTCP) { connect(sock,(struct sockaddr *)&socketSource,(socklen_t)sizeof(socketSource)); - printf("Connect successful\n"); - int isWritten = write(sock,"aaaaaa",sizeof("aaaaaa")); - printf("on écrit : %d\n",isWritten); + sendMultipleData(nombreMessage,tailleMessage,sock,socketSource,isTCP); } else { - sendMultipleData(nombreMessage,tailleMessage,sock,socketSource); + sendMultipleData(nombreMessage,tailleMessage,sock,socketSource,isTCP); } close(sock); return 0; } -int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct) +int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct, int isTCP) { int longueurEmis; char messageChar='a'; char sendingMessage[tailleMessage]; for(int i=0;i='z'?messageChar='a':messageChar++; } return 0; } \ No newline at end of file diff --git a/src/tsock.c b/src/tsock.c index 935fb99..6e2225b 100644 --- a/src/tsock.c +++ b/src/tsock.c @@ -40,7 +40,6 @@ void initStructSocket(struct sockaddr_in *socketTempStruct, int source) else { socketTempStruct->sin_addr.s_addr=INADDR_ANY; - } } @@ -50,4 +49,32 @@ void getNonOtpArgs(char ** argv, int argc, int portNumber, char ** ipAddress) *ipAddress = NULL; *ipAddress = (char *)malloc(sizeof(argv[argc-1])); strcpy(*ipAddress,argv[argc-1]); +} + +void formatText(char * actualMessage, int num, int tailleMessage, char messageChar) +{ + char numBuffer[15]; + sprintf(numBuffer, "%d", (num+1)%10000); + int numberLength=0; + while(numBuffer[numberLength] != '\0') + { + numberLength++; + } + for(int i=0;i<4-numberLength;i++) + { + actualMessage[i]=0x20; + } + for(int i=4-numberLength,j=0;i<4;i++,j++) + { + actualMessage[i]=numBuffer[j]; + } + memset(actualMessage+4, messageChar, tailleMessage); +} + +int exitMax(int var,int tailleMax){ + if(var>1500){ + printf("-l doit être <1500 \n"); + exit(EXIT_FAILURE); + } + return 0; } \ No newline at end of file