diff --git a/README.md b/README.md index 9942b2e..183e04d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # tsock Using C's standard UDP/TCP Library, send and receive data through the Internet. +Pour compiler : +``` +gcc -Wall src/*.c -o main +``` \ No newline at end of file diff --git a/header/puit.h b/header/puit.h new file mode 100644 index 0000000..011aa47 --- /dev/null +++ b/header/puit.h @@ -0,0 +1,8 @@ +#ifndef HEADER_PUIT +#define HEADER_PUIT + +#include "tsock.h" +int launchPuit(int nombreMessage); +int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct); + +#endif \ No newline at end of file diff --git a/header/source.h b/header/source.h new file mode 100644 index 0000000..c499452 --- /dev/null +++ b/header/source.h @@ -0,0 +1,8 @@ +#ifndef HEADER_SOURCE +#define HEADER_SOURCE +#include "tsock.h" + +int launchSource(int nombreMessage); +int sendMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct); + +#endif \ No newline at end of file diff --git a/tsock.h b/header/tsock.h similarity index 67% rename from tsock.h rename to header/tsock.h index d042d0c..de8238d 100644 --- a/tsock.h +++ b/header/tsock.h @@ -13,10 +13,8 @@ void setNbMessage(int * nb, int source); void printInfo(int nb, int source); -void initSocketAddr(struct sockaddr_in *socketServerTemp, int source); -void creationSocket(int *socketTemp, struct sockaddr_in *socketTempStruct); +void initStructSocket(struct sockaddr_in *socketServerTemp, int source); -#define _OE_SOCKETS #define PORT_NUM 9000 #endif \ No newline at end of file diff --git a/puit.c b/puit.c deleted file mode 100644 index 13a348c..0000000 --- a/puit.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "puit.h" - -int launchPuit(void) -{ - int sock; - char buffer[1024]; - struct sockaddr_in socketPuit; - - creationSocket(&sock,&socketPuit); - initSocketAddr(&socketPuit,0); - - if (bind(sock, (const struct sockaddr *)&socketPuit, sizeof(socketPuit)) < 0 ) - { - perror("bind failed"); - exit(EXIT_FAILURE); - } - - int n; - unsigned int lg_socketPuit = sizeof(socketPuit); - - n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketPuit,&lg_socketPuit); - buffer[n] = '\0'; - printf("Server : %s\n", buffer); - close(sock); - return 0; -} \ No newline at end of file diff --git a/puit.h b/puit.h deleted file mode 100644 index 6831361..0000000 --- a/puit.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef HEADER_PUIT -#define HEADER_PUIT - -#include "tsock.h" -int launchPuit(void); - -#endif \ No newline at end of file diff --git a/source b/source deleted file mode 100755 index 3468d3f..0000000 Binary files a/source and /dev/null differ diff --git a/source.c b/source.c deleted file mode 100644 index c331d15..0000000 --- a/source.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "source.h" - -int launchSource(void) -{ - int sock; - int lg_emis; - struct hostent *hp; - struct sockaddr_in socketSource; - if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1) - { - printf("échec création du socket\n"); - exit(1); - } - memset(&socketSource, 0, sizeof(socketSource)); - initSocketAddr(&socketSource,1); - lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketSource, sizeof(socketSource)); - printf("lgemis = %d\n",lg_emis); - close(sock); - return 0; -} \ No newline at end of file diff --git a/source.h b/source.h deleted file mode 100644 index fe2dc49..0000000 --- a/source.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef HEADER_SOURCE -#define HEADER_SOURCE - -#include "tsock.h" -int launchSource(void); - -#endif \ No newline at end of file diff --git a/main.c b/src/main.c similarity index 86% rename from main.c rename to src/main.c index 514f431..e999db0 100644 --- a/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ -#include "tsock.h" -#include "puit.h" -#include "source.h" +#include "../header/tsock.h" +#include "../header/puit.h" +#include "../header/source.h" int main (int argc, char **argv) { @@ -45,11 +45,11 @@ int main (int argc, char **argv) if(source) { printf("Source : %d\n",nb_message); - launchSource(); + launchSource(nb_message); } else { printf("Puit : %d\n",nb_message); - launchPuit(); + launchPuit(nb_message); } } \ No newline at end of file diff --git a/src/puit.c b/src/puit.c new file mode 100644 index 0000000..4f9d41c --- /dev/null +++ b/src/puit.c @@ -0,0 +1,48 @@ +#include "../header/puit.h" + +int launchPuit(int nombreMessage) +{ + int sock; + struct sockaddr_in socketPuit; + + if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1) + { + perror("[tsock] : fonction socket() : echec creation du socket\n"); + exit(EXIT_FAILURE); + } + initStructSocket(&socketPuit,0); + if (bind(sock, (const struct sockaddr *)&socketPuit, sizeof(socketPuit)) < 0 ) + { + perror("[tsock] : fonction bind() : echec du lien avec socket serveur.\n"); + exit(EXIT_FAILURE); + } + receiveMultipleData(nombreMessage,30,sock,socketPuit); + close(sock); + return 0; +} + +int receiveMultipleData(int nombreMessages, int tailleMessage, int sock, struct sockaddr_in socketStruct) +{ + char messageRecu[tailleMessage+1]; + int n, longueurRecu = sizeof(socketStruct); + if(nombreMessages < 0) + { + int i=1; + while(1) + { + 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); + i++; + } + } + else{ + for(int i=0;isin_family=AF_INET; socketTempStruct->sin_port=htons(PORT_NUM); if(source) @@ -40,14 +41,4 @@ void initSocketAddr(struct sockaddr_in *socketTempStruct, int source) socketTempStruct->sin_addr.s_addr=INADDR_ANY; } -} - -void creationSocket(int *socketTemp, struct sockaddr_in *socketTempStruct) -{ - if((*socketTemp=socket(AF_INET,SOCK_DGRAM,0)) == -1) - { - printf("échec création du socket\n"); - exit(1); - } - memset(socketTempStruct, 0, sizeof(*socketTempStruct)); } \ No newline at end of file