Commit 685f1984de3a3c938fac2aad27de8f5e37fc9389
1 parent
8d95b38e
Ajout threads
Showing
4 changed files
with
48 additions
and
6 deletions
Show diff stats
Sioux/sioux
No preview for this file type
Sioux/sioux.c
@@ -7,9 +7,14 @@ | @@ -7,9 +7,14 @@ | ||
7 | #include <string.h> | 7 | #include <string.h> |
8 | #include <netdb.h> | 8 | #include <netdb.h> |
9 | #include <arpa/inet.h> | 9 | #include <arpa/inet.h> |
10 | +#include <pthread.h> | ||
10 | 11 | ||
11 | #define BUFF_SIZE 100 | 12 | #define BUFF_SIZE 100 |
12 | 13 | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
13 | int initialisationServeur(char* service) | 18 | int initialisationServeur(char* service) |
14 | { | 19 | { |
15 | int sock_fd; | 20 | int sock_fd; |
@@ -57,15 +62,18 @@ int initialisationServeur(char* service) | @@ -57,15 +62,18 @@ int initialisationServeur(char* service) | ||
57 | return sock_fd; | 62 | return sock_fd; |
58 | } | 63 | } |
59 | 64 | ||
60 | -void reponseConnexion(int sock) | 65 | +void* reponseConnexion(void* sock) |
61 | { | 66 | { |
62 | printf("Connected\n"); | 67 | printf("Connected\n"); |
63 | char buffer[BUFF_SIZE]; | 68 | char buffer[BUFF_SIZE]; |
64 | 69 | ||
65 | - FILE* sockdial_stream = fdopen(sock, "a+"); | 70 | + FILE* sockdial_stream = fdopen(*((int*)(sock)), "a+"); |
71 | + | ||
72 | + printf("Sock : %d // Stream : %p \n", *((int*)(sock)), sockdial_stream); | ||
66 | 73 | ||
67 | while(strcmp(buffer, "EXIT\n")!=0) | 74 | while(strcmp(buffer, "EXIT\n")!=0) |
68 | { | 75 | { |
76 | + printf("WAIT\n"); | ||
69 | fgets(buffer, BUFF_SIZE, sockdial_stream); | 77 | fgets(buffer, BUFF_SIZE, sockdial_stream); |
70 | 78 | ||
71 | printf("Le message reçu : %s", buffer); | 79 | printf("Le message reçu : %s", buffer); |
@@ -79,6 +87,19 @@ void reponseConnexion(int sock) | @@ -79,6 +87,19 @@ void reponseConnexion(int sock) | ||
79 | 87 | ||
80 | } | 88 | } |
81 | 89 | ||
90 | + | ||
91 | +int lanceThread(void(* fonction) (void *), void* arg, int size) | ||
92 | +{ | ||
93 | + pthread_t thr_id; | ||
94 | + | ||
95 | + if(pthread_create(&thr_id, NULL, (void*)fonction, arg )!=0) { fprintf(stderr, "Le thread n'a pas pu être créé.\n"); return -1; } | ||
96 | + | ||
97 | + pthread_detach(thr_id); | ||
98 | + | ||
99 | + return 0; | ||
100 | +} | ||
101 | + | ||
102 | + | ||
82 | int boucleServeur(int socket, void(* fctConnex)(int)) | 103 | int boucleServeur(int socket, void(* fctConnex)(int)) |
83 | { | 104 | { |
84 | int sock_dial; | 105 | int sock_dial; |
@@ -90,8 +111,8 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | @@ -90,8 +111,8 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | ||
90 | printf("-- boucle --\n"); | 111 | printf("-- boucle --\n"); |
91 | if((sock_dial=accept(socket, NULL, NULL)) < 0){ fprintf(stderr, "Error accept dialogue\n"); return -1; } | 112 | if((sock_dial=accept(socket, NULL, NULL)) < 0){ fprintf(stderr, "Error accept dialogue\n"); return -1; } |
92 | printf("Dialogue ACCEPTED\n"); | 113 | printf("Dialogue ACCEPTED\n"); |
93 | - | ||
94 | - fctConnex(sock_dial); | 114 | + |
115 | + lanceThread((void*)fctConnex, (void*)&sock_dial, sizeof(sock_dial)); | ||
95 | } | 116 | } |
96 | } | 117 | } |
97 | 118 | ||
@@ -122,10 +143,10 @@ int main(int argc, char* argv[]) | @@ -122,10 +143,10 @@ int main(int argc, char* argv[]) | ||
122 | 143 | ||
123 | int sock_fd=initialisationServeur(port); | 144 | int sock_fd=initialisationServeur(port); |
124 | 145 | ||
125 | - if( sock_fd==-1 ) { fprintf(stderr, "Initialisaiton du serveur impossible\n"); return -1; } | 146 | + if( sock_fd==-1 ) { fprintf(stderr, "Initialisation du serveur impossible\n"); return -1; } |
126 | 147 | ||
127 | 148 | ||
128 | - boucleServeur(sock_fd, reponseConnexion); | 149 | + boucleServeur(sock_fd, (void*)&reponseConnexion); |
129 | return 0; | 150 | return 0; |
130 | } | 151 | } |
131 | 152 |
No preview for this file type
@@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <pthread.h> | ||
4 | + | ||
5 | + | ||
6 | +int lanceThread(void(* fonction) (void *), void* arg, int size) | ||
7 | +{ | ||
8 | + pthread_t thr_id; | ||
9 | + | ||
10 | + if(pthread_create(&thr_id, NULL, fonction, arg)!=0) { fprintf(stderr, "Le thread n'a pas pu être créé.\n"); return -1; } | ||
11 | + | ||
12 | + pthread_detach(thr_id); | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | +} | ||
17 | + | ||
18 | +int main(void) | ||
19 | +{ | ||
20 | + | ||
21 | +} |