-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
402 lines (383 loc) · 11 KB
/
server.c
File metadata and controls
402 lines (383 loc) · 11 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <netdb.h>
#include <arpa/inet.h>
//info de client
struct _client
{
char ipAddress[40];
int port;
char name[40];
} tcpClients[4];
int nbClients;
int fsmServer;
int deck[13]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int tableCartes[4][8];
char *nomcartes[]=
{"Sebastian Moran", "irene Adler", "inspector Lestrade",
"inspector Gregson", "inspector Baynes", "inspector Bradstreet",
"inspector Hopkins", "Sherlock Holmes", "John Watson", "Mycroft Holmes",
"Mrs. Hudson", "Mary Morstan", "James Moriarty"};
int joueurCourant;
int horsTour[4]={0,0,0,0};
int gId=-1, guiltSel=-1, joueurSel=-1, objetSel=-1;
void error(const char *msg)
{
perror(msg);
exit(1);
}
//change the order in deck
void melangerDeck()
{
int i;
int index1,index2,tmp;
srand(time(NULL));
for (i=0;i<1000;i++)
{
index1=rand()%13;
index2=rand()%13;
tmp=deck[index1];
deck[index1]=deck[index2];
deck[index2]=tmp;
}
}
// remplir int tablecarte
void createTable()
{
// Le joueur 0 possede les cartes d'indice 0,1,2
// Le joueur 1 possede les cartes d'indice 3,4,5
// Le joueur 2 possede les cartes d'indice 6,7,8
// Le joueur 3 possede les cartes d'indice 9,10,11
// Le coupable est la carte d'indice 12
int i,j,c;
//initialise la table
for (i=0;i<4;i++)
for (j=0;j<8;j++)
tableCartes[i][j]=0;
for (i=0;i<4;i++)
{
for (j=0;j<3;j++)
{
c=deck[i*3+j];
switch (c)
{
case 0: // Sebastian Moran
tableCartes[i][7]++;
tableCartes[i][2]++;
break;
case 1: // Irene Adler
tableCartes[i][7]++;
tableCartes[i][1]++;
tableCartes[i][5]++;
break;
case 2: // Inspector Lestrade
tableCartes[i][3]++;
tableCartes[i][6]++;
tableCartes[i][4]++;
break;
case 3: // Inspector Gregson
tableCartes[i][3]++;
tableCartes[i][2]++;
tableCartes[i][4]++;
break;
case 4: // Inspector Baynes
tableCartes[i][3]++;
tableCartes[i][1]++;
break;
case 5: // Inspector Bradstreet
tableCartes[i][3]++;
tableCartes[i][2]++;
break;
case 6: // Inspector Hopkins
tableCartes[i][3]++;
tableCartes[i][0]++;
tableCartes[i][6]++;
break;
case 7: // Sherlock Holmes
tableCartes[i][0]++;
tableCartes[i][1]++;
tableCartes[i][2]++;
break;
case 8: // John Watson
tableCartes[i][0]++;
tableCartes[i][6]++;
tableCartes[i][2]++;
break;
case 9: // Mycroft Holmes
tableCartes[i][0]++;
tableCartes[i][1]++;
tableCartes[i][4]++;
break;
case 10: // Mrs. Hudson
tableCartes[i][0]++;
tableCartes[i][5]++;
break;
case 11: // Mary Morstan
tableCartes[i][4]++;
tableCartes[i][5]++;
break;
case 12: // James Moriarty
tableCartes[i][7]++;
tableCartes[i][1]++;
break;
}
}
}
}
// affiche deck
void printDeck()
{
int i,j;
for (i=0;i<13;i++)
printf("%d %s\n",deck[i],nomcartes[deck[i]]);
for (i=0;i<4;i++)
{
for (j=0;j<8;j++)
printf("%2.2d ",tableCartes[i][j]);
puts("");
}
}
//connecter client
void printClients()
{
int i;
for (i=0;i<nbClients;i++)
printf("%d: %s %5.5d %s\n",i,tcpClients[i].ipAddress,
tcpClients[i].port,
tcpClients[i].name);
}
// trouver le numéro du client à partir de son nom
int findClientByName(char *name)
{
int i;
for (i=0;i<nbClients;i++)
if (strcmp(tcpClients[i].name,name)==0)
return i;
return -1;
}
//send message à un client spécifique
void sendMessageToClient(char *clientip,int clientport,char *mess)
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server = gethostbyname(clientip);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(clientport);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
printf("ERROR connecting\n");
exit(1);
}
sprintf(buffer,"%s\n",mess);
n = write(sockfd,buffer,strlen(buffer));
close(sockfd);
}
// envoie message à tout les clients
void broadcastMessage(char *mess)
{
int i;
for (i=0;i<nbClients;i++)
sendMessageToClient(tcpClients[i].ipAddress,
tcpClients[i].port,
mess);
}
//passer au joueur suivant
void nextplayer(char *reply){
joueurCourant++;
if(joueurCourant == 4)
joueurCourant = 0;
sprintf(reply,"M %d %d %d %d %d",joueurCourant,horsTour[0],horsTour[1],horsTour[2],horsTour[3]);
broadcastMessage(reply);
}
//annoncer le resultat
void getwinner(char *reply){
printf("Le joueur %s est le gagnant!\nLe coupable est %s\n",tcpClients[gId].name, nomcartes[deck[12]]);
sprintf(reply,"M %d",-1);
broadcastMessage(reply);
sprintf(reply,"Le joueur %s est le gagnant!\nLe coupable est %s\n",
tcpClients[gId].name,nomcartes[deck[12]]);
broadcastMessage(reply);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
int i;
char com;
char clientIpAddress[256], clientName[256];
int clientPort;
int id;
char reply[256];
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
//créer unsocket du type TCP
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
//bind dire à système que le serveur a ouvert un socket et spécifie son porte
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
//
printDeck();
melangerDeck();
createTable();
printDeck();
joueurCourant=0; // premier joueur
for (i=0;i<4;i++)
{
strcpy(tcpClients[i].ipAddress,"localhost");//sur le meme machine par defaut
tcpClients[i].port=-1;
strcpy(tcpClients[i].name,"-");
}
// boucle de serveur TCP
//accept , faire des traitements séquenciellement pas besoin de fork, thread.(message trop court))
while (1)
{
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
//lire les messages
n = read(newsockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
//afficher "j'ai reçu, et de qui"
printf("Received packet from %s:%d\nData: [%s]\n\n",
inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), buffer);
//fsmserveur machine d'état fini
if (fsmServer==0)
{
switch (buffer[0])
{ //message du client
case 'C':
sscanf(buffer,"%c %s %d %s", &com, clientIpAddress, &clientPort, clientName);
printf("COM=%c ipAddress=%s port=%d name=%s\n",com, clientIpAddress, clientPort, clientName);
// fsmServer==0 alors j'attends les connexions de tous les joueurs
strcpy(tcpClients[nbClients].ipAddress,clientIpAddress);
tcpClients[nbClients].port=clientPort;
strcpy(tcpClients[nbClients].name,clientName);
nbClients++;
printClients();
// rechercher l'id du joueur qui vient de se connecter
id=findClientByName(clientName);
printf("id=%d\n",id);
// lui envoyer un message personnel pour lui communiquer son id
sprintf(reply,"I %d",id);
sendMessageToClient(tcpClients[id].ipAddress, tcpClients[id].port,reply);
// Envoyer un message broadcast pour communiquer a tout le monde la liste des joueurs actuellement
sprintf(reply,"L %s %s %s %s", tcpClients[0].name, tcpClients[1].name, tcpClients[2].name, tcpClients[3].name);
broadcastMessage(reply);
// Si le nombre de joueurs atteint 4, alors on peut lancer le jeu
if (nbClients==4)
{
// On envoie ses cartes aux joueurs, ainsi que la ligne qui lui correspond dans tableCartes
for( i=0; i<4; i++){
// Envoie les cartes
sprintf(reply,"D %d %d %d",deck[i*3], deck[i*3+1], deck[i*3+2]);
sendMessageToClient(tcpClients[i].ipAddress, tcpClients[i].port, reply);
//Envoie la ligne
for(int j=0; j<8; j++){
sprintf(reply,"V %d %d %d", i, j, tableCartes[i][j]);
sendMessageToClient(tcpClients[i].ipAddress, tcpClients[i].port, reply);
}
}
// On envoie enfin un message a tout le monde pour definir qui est le joueur courant=0
sprintf(reply,"M %d %d %d %d %d",joueurCourant,horsTour[0],horsTour[1],horsTour[2],horsTour[3]);
broadcastMessage(reply);
fsmServer=1;
}
break;
}
}
else if (fsmServer==1){
int i,j=0;
switch (buffer[0])
{
case 'A':
nextplayer(reply);
break;
case 'G':
sscanf(buffer,"G %d %d",&gId, &guiltSel);
printf("Le joueur %d a choisi le coupable %d\n", gId, guiltSel);
if(deck[12] == guiltSel){
getwinner(reply);
fsmServer=0;
}
else {
sprintf(reply,"Le joueur %s est mis hors du tour.", tcpClients[gId].name);
broadcastMessage(reply);
printf("%s\n",reply);
horsTour[gId] = 1;
for(i=0;i<4;i++){
if(horsTour[i]==1)
j++;
}
if(j==3){
for(i=0;i<4;i++){
if(horsTour[i]==0)
gId=i;
}
getwinner(reply);
fsmServer=0;
break;
}
nextplayer(reply);
}
break;
case 'O': // Qui a combien
sscanf(buffer,"O %d %d", &gId, &objetSel);
for( i=0; i<nbClients; i++){
sprintf(reply,"V %d %d %d", i, objetSel, tableCartes[i][objetSel] >= 1? 100: 0);
broadcastMessage(reply);
}
nextplayer(reply);
break;
case 'S': // combien
sscanf(buffer,"S %d %d %d", &gId, &joueurSel, &objetSel);
printf("Joueur %d demande le nb du objet %d que le joueur %d possede\n", gId, objetSel, joueurSel);
sprintf(reply,"V %d %d %d", joueurSel, objetSel, tableCartes[joueurSel][objetSel]);
broadcastMessage(reply);
nextplayer(reply);
break;
default:
break;
}
}
close(newsockfd);
}
close(sockfd);
return 0;
}