-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveur.c
217 lines (190 loc) · 4.73 KB
/
serveur.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>
#define NB_CLIENTS_MAX 1024
int serveur = 0,
maxClients = NB_CLIENTS_MAX,
s_ecoute = 0,
nbClients = 0;
pthread_t * clients = NULL;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/*
*
* Structure de retour de la fonction de compte
*
*/
typedef struct lettre {
unsigned long v;
unsigned long c;
} lettre;
/*
*
* Compte le nombre de voyelles et de consonnes
*
*/
int findFreeThread() {
int indice = 0;
while(indice < maxClients && clients[indice]!=0) {
indice++;
}
fflush(stdin);
return indice;
}
/*
*
* Compte le nombre de voyelles et de consonnes
*
*/
lettre countLetters(char * s) {
lettre l;
unsigned i = 0;
l.v = 0;
l.c = 0;
while(s[i] != 0) {
if(isalpha(s[i])) {
if(strchr("eaiouyEAIOUY", s[i])) {
(l.v)++;
} else {
(l.c)++;
}
}
i++;
}
return l;
}
/*
*
* Thread execute a l'arrivee d'un nouveau client
*
*/
void * threadClient(void * args) {
char buf[1500],
renvoi[1500];
int socket = *((int*) args),
numero = ((int*) args)[1],
client = 1;
pthread_mutex_lock(&mutex); // verrouillage
((int*) args)[0] = 0;
pthread_mutex_unlock(&mutex); // deverrouillage
while(client) {
usleep(1); // on laisse la main
recv(socket,buf,sizeof(buf),0);
printf("Client's request:\n>>> %s\n",buf);
lettre l = countLetters(buf);
bzero(renvoi,sizeof(renvoi));
if(buf[0]=='+')
sprintf(renvoi, "Number of consonants: %lu\n", l.c);
else if(buf[0]=='-')
sprintf(renvoi, "Number of vowels: %lu\n", l.v);
else if(buf[0]=='/') {
client = 0;
sprintf(renvoi, "Session ended.\n");
printf(renvoi, "Session ended.\n");
} else if(buf[0]=='.') {
client = 0;
serveur = 0;
close(s_ecoute);// arret de l'ecoute
sprintf(renvoi, "Session ended.\n>>> Server stopped.\n");
} else {
sprintf(renvoi, "Unknown request!\n");
}
send(socket,renvoi,strlen(renvoi),0);
}
nbClients--;
clients[numero] = 0;
close(socket);
pthread_exit(NULL);
}
/*
*
* Thread a l'ecoute de l'arrivee de nouveau clients
*
*/
void * threadEcoute(void * args) {
int scom,
lg_app;
struct sockaddr_in adr;
struct sockaddr_storage recep;
char host[1024],
service[20];
s_ecoute=socket(AF_INET,SOCK_STREAM,0);
printf("Socket created.\n");
adr.sin_family=AF_INET;
adr.sin_port=htons(*(int*)args);
adr.sin_addr.s_addr=INADDR_ANY;
if(bind(s_ecoute,(struct sockaddr *)&adr,sizeof(struct sockaddr_in)) !=0) {
printf("Bind problem on v4\n");
exit(1);
}
if(listen(s_ecoute,5) != 0) {
printf("Listen problem.\n"); exit(1);
}
//fcntl(s_ecoute, F_SETFL, O_NONBLOCK); // rend la socket non bloquante
printf("Waiting for client...\n");
serveur = 1;
int tab[2] = {0};
while(serveur) { // tant que le serveur n'a pas recu d'ordre d'extinction
// on ecoute et allloue un thread
if(nbClients < maxClients) {
scom = accept(s_ecoute,(struct sockaddr *)&recep, (socklen_t *)&lg_app);
getnameinfo((struct sockaddr *)&recep,sizeof(recep), host, sizeof(host),service,sizeof(service),0);
printf("Received %s from %s.\n", host, service);
int indice = findFreeThread();
if(indice < maxClients) {
tab[0] = scom;
tab[1] = indice;
if( pthread_create( &clients[indice], NULL, threadClient, (void*) tab) < 0 ) {
perror("Cannot create client thread.\n");
} else {
nbClients++;
}
} else {
printf("Client refused.\n");
close(scom);
}
}
usleep(1);
}
pthread_exit(NULL);
}
/*
*
* Programme serveur multi-threads
*
*/
int main(int argc, char * argv[]) {
if(argc < 2) {
printf("\nUsage:\n serveur <port> [NB_MAX_CLIENTS]\n\n");
} else {
if(argc > 2)
maxClients = strtoul(argv[2], 0, 0);
clients = (pthread_t *)malloc(sizeof(pthread_t)*maxClients); // allocation des threads
pthread_t ecoute;
int port = atoi(argv[1]);
if( pthread_create( &ecoute, NULL, threadEcoute, (void*)&port) < 0 ) {
perror("Cannot throw listening thread.\n");
}
serveur = 1;
while(serveur) {
usleep(1);
}
close(s_ecoute); // arret du socket d'ecoute
pthread_cancel(ecoute); // arret du thread d'ecoute
int i = 0;
for(i = 0 ; i < maxClients ; ++i) {
if(clients[i] != 0){
pthread_join(clients[i], 0); // attente
}
}
free(clients); // liberation des threads
}
return 0;
}