-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
570 lines (503 loc) · 19.2 KB
/
server.c
File metadata and controls
570 lines (503 loc) · 19.2 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define TRUE 1
#define FALSE 0
#define MAXSIZE 1024 /*max buffer size*/
#define SERV_PORT 8080 /*port*/
#define LISTENQ 3 /*maximum number of client connections*/
#define MAX_CLIENTS_COUNT 20
/* declaring variables*/
int server_fd, new_socket ; //sockets
struct sockaddr_in address ; // for tcp connection
int addrlen = sizeof(address) ;
fd_set client_fds ; /* set of file descriptors of clients */
int max_fds ;
int clientSockets[MAX_CLIENTS_COUNT] = {0} ;
char *wait_message = "wait for finding opponent(s) ...";
int client_giving_port = 8001;
char *status[MAX_CLIENTS_COUNT];
/* ------------------ */
void Request_Handler(char *info, int index)
{
//printf("message get from client in Request_Handler : \"%s\"\n", info);
if (strcmp(info, "2") == 0)
{
status[index] = "2";
}
else if (strcmp(info, "3") == 0)
{
status[index] = "3";
}
else if (strcmp(info, "4") == 0)
{
status[index] = "4";
}
write(1, "Client requested a ", 19);
write(1, status[index], strlen(status[index]));
write(1, "-Group\n", 7);
//printf("Client requested a %s-Group\n", status[index]);
}
void Connect_To_1_Opponent(int index, int i)
{
//perror("get inside Connect_To_1_Opponent() function");
int port;
int data_send1, data_send2;
char str_port[5];
char turn1[2];
char turn2[2];
char wspace[2];
char sending_message1[10];
char sending_message2[10];
memset(sending_message1, 0, sizeof(sending_message1));
memset(sending_message2, 0, sizeof(sending_message2));
port = client_giving_port;
client_giving_port++;
sprintf(str_port, "%d", port);
strcpy(turn1, "1");
strcpy(turn2, "2");
strcpy(wspace, " ");
strcat(sending_message1, str_port);
strcat(sending_message1, wspace);
strcat(sending_message1, turn1);
int len1 = strlen(sending_message1);
strcat(sending_message2, str_port);
strcat(sending_message2, wspace);
strcat(sending_message2, turn2);
int len2 = strlen(sending_message2);
//printf("sending_message1 is : \"%s\" and len1 is : %d on fd : %d\n", sending_message1, len1, clientSockets[index]);
//printf("sending_message2 is : \"%s\" and len2 is : %d on fd : %d and i = %d\n", sending_message2, len2, clientSockets[i], i);
data_send2 = sendto(clientSockets[i], sending_message2, len2, 0, NULL, 0);
data_send1 = sendto(clientSockets[index], sending_message1, len1, 0, NULL, 0);
//data_send1 = send(clientSockets[index], sending_message1, len1, 0);
if (data_send2 < 0 && data_send1 < 0)
{
//perror("sending opponent data failed");
write(2, "sending opponent data failed\n", 29);
}
else
{
//printf("data_send1 is : \"%d\" and data_send2 is : %d\n", data_send1, data_send2);
//printf("two clients are connecting...\n");
write(1, "two clients are connecting...\n", 30);
close(clientSockets[i]);
close(clientSockets[index]);
clientSockets[i] = 0;
clientSockets[index] = 0;
}
}
void Connect_To_2_Opponent(int index, int i, int j)
{
int port;
int data_send1, data_send2, data_send3;
char str_port[5];
char turn1[2];
char turn2[2];
char turn3[2];
char wspace[2];
char sending_message1[10];
char sending_message2[10];
char sending_message3[10];
memset(sending_message1, 0, sizeof(sending_message1));
memset(sending_message2, 0, sizeof(sending_message2));
memset(sending_message3, 0, sizeof(sending_message3));
port = client_giving_port;
client_giving_port++;
sprintf(str_port, "%d", port);
strcpy(turn1, "1");
strcpy(turn2, "2");
strcpy(turn3, "3");
strcpy(wspace, " ");
strcat(sending_message1, str_port);
strcat(sending_message1, wspace);
strcat(sending_message1, turn1);
int len1 = strlen(sending_message1);
strcat(sending_message2, str_port);
strcat(sending_message2, wspace);
strcat(sending_message2, turn2);
int len2 = strlen(sending_message2);
strcat(sending_message3, str_port);
strcat(sending_message3, wspace);
strcat(sending_message3, turn3);
int len3 = strlen(sending_message3);
//printf("sending_message1 is : \"%s\" and len1 is : %d on fd : %d\n", sending_message1, len1, clientSockets[index]);
//printf("sending_message2 is : \"%s\" and len2 is : %d on fd : %d and i = %d\n", sending_message2, len2, clientSockets[i], i);
data_send2 = sendto(clientSockets[i], sending_message2, len2, 0, NULL, 0);
data_send3 = sendto(clientSockets[j], sending_message3, len3, 0, NULL, 0);
data_send1 = sendto(clientSockets[index], sending_message1, len1, 0, NULL, 0);
//data_send1 = send(clientSockets[index], sending_message1, len1, 0);
if (data_send2 < 0 && data_send3 < 0 && data_send1 < 0)
{
//perror("sending opponent data failed");
write(2, "sending opponent data failed\n", 29);
}
else
{
//printf("data_send1 is : \"%d\" and data_send2 is : %d\n", data_send1, data_send2);
//printf("two clients are connecting...\n");
write(1, "three clients are connecting...\n", 32);
close(clientSockets[i]);
close(clientSockets[j]);
close(clientSockets[index]);
clientSockets[i] = 0;
clientSockets[j] = 0;
clientSockets[index] = 0;
}
}
void Connect_To_3_Opponent(int index, int i, int j, int k)
{
int port;
int data_send1, data_send2, data_send3, data_send4;
char str_port[5];
char turn1[2];
char turn2[2];
char turn3[2];
char turn4[2];
char wspace[2];
char sending_message1[10];
char sending_message2[10];
char sending_message3[10];
char sending_message4[10];
memset(sending_message1, 0, sizeof(sending_message1));
memset(sending_message2, 0, sizeof(sending_message2));
memset(sending_message3, 0, sizeof(sending_message3));
memset(sending_message4, 0, sizeof(sending_message4));
port = client_giving_port;
client_giving_port++;
sprintf(str_port, "%d", port);
strcpy(turn1, "1");
strcpy(turn2, "2");
strcpy(turn3, "3");
strcpy(turn4, "4");
strcpy(wspace, " ");
strcat(sending_message1, str_port);
strcat(sending_message1, wspace);
strcat(sending_message1, turn1);
int len1 = strlen(sending_message1);
strcat(sending_message2, str_port);
strcat(sending_message2, wspace);
strcat(sending_message2, turn2);
int len2 = strlen(sending_message2);
strcat(sending_message3, str_port);
strcat(sending_message3, wspace);
strcat(sending_message3, turn3);
int len3 = strlen(sending_message3);
strcat(sending_message4, str_port);
strcat(sending_message4, wspace);
strcat(sending_message4, turn4);
int len4 = strlen(sending_message4);
//printf("sending_message1 is : \"%s\" and len1 is : %d on fd : %d\n", sending_message1, len1, clientSockets[index]);
//printf("sending_message2 is : \"%s\" and len2 is : %d on fd : %d and i = %d\n", sending_message2, len2, clientSockets[i], i);
data_send2 = sendto(clientSockets[i], sending_message2, len2, 0, NULL, 0);
data_send3 = sendto(clientSockets[j], sending_message3, len3, 0, NULL, 0);
data_send4 = sendto(clientSockets[k], sending_message4, len4, 0, NULL, 0);
data_send1 = sendto(clientSockets[index], sending_message1, len1, 0, NULL, 0);
//data_send1 = send(clientSockets[index], sending_message1, len1, 0);
if (data_send2 < 0 && data_send3 < 0 && data_send4 < 0 && data_send1 < 0)
{
//perror("sending opponent data failed");
write(2, "sending opponent data failed\n", 29);
}
else
{
//printf("data_send1 is : \"%d\" and data_send2 is : %d\n", data_send1, data_send2);
//printf("two clients are connecting...\n");
write(1, "four clients are connecting...\n", 31);
close(clientSockets[i]);
close(clientSockets[j]);
close(clientSockets[k]);
close(clientSockets[index]);
clientSockets[i] = 0;
clientSockets[j] = 0;
clientSockets[k] = 0;
clientSockets[index] = 0;
}
}
void Find_Opponent(int index)
{
//printf("Find_Opponent opened \n");
int len_wait_mes = strlen(wait_message) + 1;
int num;
num = atoi(status[index]);
//printf("wants to go inside the switch case \n");
//printf("num is : \"%d\" \n", num);
if (num == 2)
{
//printf("in num == 2 \n");
for (int i = 0; i < MAX_CLIENTS_COUNT; i++)
{
if (i == index)
continue;
else
{
if (clientSockets[i] != 0 && strcmp(status[i], status[index]) == 0)
{
//printf("find a copponent !!!! \n");
Connect_To_1_Opponent(index, i);
write(1, "-------- start a 2-player game ------- \n", 40);
//printf("-------- start a 2-person game ------- \n");
return;
}
}
}
if (send(clientSockets[index], wait_message, len_wait_mes, 0) < 0)
{
//perror("sending find opponent message failed");
write(2, "sending find opponent message failed\n", 37);
}
//printf("the sent mwssage : %s \n", wait_message);
}
else if (num == 3)
{
//printf("in num == 3 \n");
for (int i = 0; i < MAX_CLIENTS_COUNT; i++)
{
if (i == index)
continue;
else
{
if (clientSockets[i] != 0 && strcmp(status[i], status[index]) == 0)
for (int j = 0; j < MAX_CLIENTS_COUNT; j++)
{
if (j == index || j == i)
continue;
else
{
if (clientSockets[j] != 0 && strcmp(status[j], status[index]) == 0)
{
Connect_To_2_Opponent(index, i, j);
//printf("-------- start a 3-person game ------- \n");
write(1, "-------- start a 3-player game ------- \n", 40);
return;
}
}
}
}
}
if (send(clientSockets[index], wait_message, len_wait_mes, 0) < 0)
{
//perror("sending find opponent message failed");
write(2, "sending find opponent message failed\n", 37);
}
//printf("the sent mwssage : %s \n", wait_message);
}
else if (num == 4)
{
//printf("in num == 4 \n");
for (int i = 0; i < MAX_CLIENTS_COUNT; i++)
{
if (i == index)
continue;
else
{
if (clientSockets[i] != 0 && strcmp(status[i], status[index]) == 0)
for (int j = 0; j < MAX_CLIENTS_COUNT; j++)
{
if (j == index || j == i)
continue;
else
{
if (clientSockets[j] != 0 && strcmp(status[j], status[index]) == 0)
for (int k = 0; k < MAX_CLIENTS_COUNT; k++)
{
if (k == index || k == i || k == j)
continue;
else
{
if (clientSockets[k] != 0 && strcmp(status[k], status[index]) == 0)
{
Connect_To_3_Opponent(index, i, j, k);
//printf("-------- start a 4-person game ------- \n");
write(1, "-------- start a 4-player game ------- \n", 40);
return;
}
}
}
}
}
}
}
if (send(clientSockets[index], wait_message, len_wait_mes, 0) < 0)
{
//perror("sending find opponent message failed");
write(2, "sending find opponent message failed\n", 37);
}
}
}
void Create_TCP_Server_Port()
{
int opt = 1;
/* creating socket with IPv4 protocol and TCP type */
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
{
//perror("socket failed");
write(2, "socket failed\n", 14);
exit(EXIT_FAILURE);
}
/* set master socket to allow multiple connections */
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
//perror("setsockopt failed");
write(2, "setsockopt failed\n", 18);
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET; // for IPv4 protocol
address.sin_addr.s_addr = INADDR_ANY; // to bind the server to the localhost "127.0.0.1"
address.sin_port = htons(SERV_PORT); // host format to network format convertor
/* binding socket to the port [8080] in localhost*/
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
//perror("bind failed");
write(2, "bind failed\n", 12);
exit(EXIT_FAILURE);
}
/* listening */
if (listen(server_fd, LISTENQ) < 0)
{
//perror("listening failed");
write(2, "listening failed\n", 17);
exit(EXIT_FAILURE);
}
}
int main(int argc, char const *argv[])
{
int event, valread;
int sd;
Create_TCP_Server_Port();
char buffer[MAXSIZE+1]; //data buffer of 1K
char str[MAXSIZE + 1];
/* welcome message */
char *message = "Hi you connected to server successfully \r\n";
while(TRUE)
{
/* clear the descriptor set */
FD_ZERO(&client_fds);
/* set server_fd in readset */
FD_SET(server_fd, &client_fds);
/* set max_fds */
max_fds = server_fd;
/* add child sockets to set */
for (int i = 0; i < MAX_CLIENTS_COUNT; i++)
{
//socket descriptor
sd = clientSockets[i];
//if valid socket descriptor then add to read list
if (sd > 0)
FD_SET(sd, &client_fds);
//highest file descriptor number, need it for the select function
if (sd > max_fds)
max_fds = sd;
}
/* wait for an activity on one of the sockets , timeout is NULL ,
* so wait indefinitely */
event = select(max_fds + 1, &client_fds, NULL, NULL, NULL);
if ((event < 0) && (errno != EINTR))
{
//perror("select() failed");
write(2, "select() failed\n", 16);
}
/* If something happened on the master socket ,
* then its an incoming connection */
if (FD_ISSET(server_fd, &client_fds))
{
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
//perror("accepting faild");
write(2, "accepting faild\n", 16);
exit(EXIT_FAILURE);
}
/* inform user of socket number - used in send and receive commands */
//printf("New connection , socket fd is %d , ip is : %s , port : %d\n " , new_socket ,
// inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
write(1, "New connection , socket fd is ", 30);
sprintf(str, "%d", new_socket);
write(1, str, strlen(str));
memset(str, 0, sizeof(str));
write(1, " , ip is : ", 11);
write(1, inet_ntoa(address.sin_addr), strlen(inet_ntoa(address.sin_addr)));
write(1, " , port ", 8);
sprintf(str, "%d", ntohs(address.sin_port));
write(1, str, strlen(str));
memset(str, 0, sizeof(str));
write(1, " \n", 2);
/* send new connection greeting message */
if (send(new_socket, message, strlen(message), 0) != strlen(message))
{
//perror("Welcome message not sent !");
write(2, "Welcome message not sent !\n", 27);
}
else
//puts("Welcome message sent successfully");
write(1, "Welcome message sent successfully\n", 34);
/* add new socket to array of sockets */
for (int i = 0; i < MAX_CLIENTS_COUNT; i++)
{
//if position is empty
if (clientSockets[i] == 0)
{
clientSockets[i] = new_socket;
//printf("Adding to list of sockets as %d\n", i);
write(1, "Adding to list of sockets as ", 29);
sprintf(str, "%d", i);
write(1, str, strlen(str));
memset(str, 0, sizeof(str));
write(1, "\n", 1);
break;
}
}
}
/* else its some IO operation on some other socket */
for (int i = 0; i < MAX_CLIENTS_COUNT; i++)
{
sd = clientSockets[i];
if (FD_ISSET(sd, &client_fds))
{
/* Check if it was for closing , and also read the
* incoming message */
if ((valread = read(sd, buffer, 1024)) == 0)
{
/* Somebody disconnected , get his details and print */
getpeername(sd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
//printf("Host disconnected , ip %s , port %d \n",
// inet_ntoa(address.sin_addr), ntohs(address.sin_port));
write(1, "Host disconnected , ip : ", 25);
write(1, inet_ntoa(address.sin_addr), strlen(inet_ntoa(address.sin_addr)));
write(1, " , port ", 8);
sprintf(str, "%d", ntohs(address.sin_port));
write(1, str, strlen(str));
memset(str, 0, sizeof(str));
write(1, " \n", 2);
/* Close the socket and mark as 0 in list for reuse */
close(sd);
clientSockets[i] = 0;
}
/* Echo back the message that came in */
else
{
/* set the string terminating NULL byte on the end
* of the data read */
buffer[valread] = '\0';
//send(sd, buffer, strlen(buffer), 0);
//printf("the received message is: \"%s\" \n", buffer);
write(1, "the received message is: \"", 26);
write(1, buffer, strlen(buffer));
//write(1, buffer, strlen(buffer) - 2); for telnet command
write(1, "\" \n", 3);
Request_Handler(buffer, i);
Find_Opponent(i);
}
}
}
}
return 0;
}