From 685f1984de3a3c938fac2aad27de8f5e37fc9389 Mon Sep 17 00:00:00 2001 From: Speedclocker Date: Tue, 12 Mar 2019 15:56:31 +0100 Subject: [PATCH] Ajout threads --- Sioux/sioux | Bin 13912 -> 0 bytes Sioux/sioux.c | 33 +++++++++++++++++++++++++++------ Sioux/sioux.o | Bin 0 -> 6544 bytes Sioux/threadsSocket.c | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 Sioux/sioux.o create mode 100644 Sioux/threadsSocket.c diff --git a/Sioux/sioux b/Sioux/sioux index 349f38b..8a9bb1f 100755 Binary files a/Sioux/sioux and b/Sioux/sioux differ diff --git a/Sioux/sioux.c b/Sioux/sioux.c index 7adeaad..bcf5058 100644 --- a/Sioux/sioux.c +++ b/Sioux/sioux.c @@ -7,9 +7,14 @@ #include #include #include +#include #define BUFF_SIZE 100 + + + + int initialisationServeur(char* service) { int sock_fd; @@ -57,15 +62,18 @@ int initialisationServeur(char* service) return sock_fd; } -void reponseConnexion(int sock) +void* reponseConnexion(void* sock) { printf("Connected\n"); char buffer[BUFF_SIZE]; - FILE* sockdial_stream = fdopen(sock, "a+"); + FILE* sockdial_stream = fdopen(*((int*)(sock)), "a+"); + + printf("Sock : %d // Stream : %p \n", *((int*)(sock)), sockdial_stream); while(strcmp(buffer, "EXIT\n")!=0) { + printf("WAIT\n"); fgets(buffer, BUFF_SIZE, sockdial_stream); printf("Le message reçu : %s", buffer); @@ -79,6 +87,19 @@ void reponseConnexion(int sock) } + +int lanceThread(void(* fonction) (void *), void* arg, int size) +{ + pthread_t thr_id; + + if(pthread_create(&thr_id, NULL, (void*)fonction, arg )!=0) { fprintf(stderr, "Le thread n'a pas pu être créé.\n"); return -1; } + + pthread_detach(thr_id); + + return 0; +} + + int boucleServeur(int socket, void(* fctConnex)(int)) { int sock_dial; @@ -90,8 +111,8 @@ int boucleServeur(int socket, void(* fctConnex)(int)) printf("-- boucle --\n"); if((sock_dial=accept(socket, NULL, NULL)) < 0){ fprintf(stderr, "Error accept dialogue\n"); return -1; } printf("Dialogue ACCEPTED\n"); - - fctConnex(sock_dial); + + lanceThread((void*)fctConnex, (void*)&sock_dial, sizeof(sock_dial)); } } @@ -122,10 +143,10 @@ int main(int argc, char* argv[]) int sock_fd=initialisationServeur(port); - if( sock_fd==-1 ) { fprintf(stderr, "Initialisaiton du serveur impossible\n"); return -1; } + if( sock_fd==-1 ) { fprintf(stderr, "Initialisation du serveur impossible\n"); return -1; } - boucleServeur(sock_fd, reponseConnexion); + boucleServeur(sock_fd, (void*)&reponseConnexion); return 0; } diff --git a/Sioux/sioux.o b/Sioux/sioux.o new file mode 100644 index 0000000..fd88f9f Binary files /dev/null and b/Sioux/sioux.o differ diff --git a/Sioux/threadsSocket.c b/Sioux/threadsSocket.c new file mode 100644 index 0000000..29a1931 --- /dev/null +++ b/Sioux/threadsSocket.c @@ -0,0 +1,21 @@ +#include +#include +#include + + +int lanceThread(void(* fonction) (void *), void* arg, int size) +{ + pthread_t thr_id; + + if(pthread_create(&thr_id, NULL, fonction, arg)!=0) { fprintf(stderr, "Le thread n'a pas pu être créé.\n"); return -1; } + + pthread_detach(thr_id); + + + +} + +int main(void) +{ + +} -- libgit2 0.21.2