-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
611 lines (532 loc) · 15.2 KB
/
server.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <signal.h>
#include<time.h>
#define MAX_CLIENTS 100
#define BUFFER_SZ 2048
static _Atomic unsigned int cli_count = 0;
static int uid = 10;
/* Client structure */
typedef struct{
struct sockaddr_in address;
int sockfd;
int uid;
char name[32];
char mode[4];
char partner[32];
} client_t;
//maurya
char qtype[2048];
char qtext[2048];
char qans[2048];
char qexplanation[2048];
char ans_out[2048];
int n_questions(char *filename){
FILE *fp;
int count = 1;
char c;
fp = fopen(filename, "r");
if (fp == NULL) return 0;
for (c = getc(fp); c != EOF; c = getc(fp)) if (c == '\n') count = count + 1;
fclose(fp);
return count/6;
}
void get_question(int topic){
char filename[100];
// topic == 1: "Threads";
// topic == 2: "Memory Management";
// topic == 3: "Scheduling";
// as operating with strings (char *) in C is pathetic
if(topic == 1) strcpy(filename,"Threads.txt");
else if(topic == 2) strcpy(filename,"Memory Management.txt");
else if(topic == 3) strcpy(filename,"Scheduling.txt");
int max_questions = n_questions(filename);
srand(time(0));
int q_number = 1+ rand()%max_questions;
FILE *file = fopen(filename, "r");
int linecount = 1;
char line[2048];
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (linecount == 6*q_number-4) strcpy(qtype, line);
else if (linecount == 6*q_number-3) strcpy(qtext, line);
else if (linecount == 6*q_number-2) strcpy(qans, line);
else if (linecount == 6*q_number-1){
strcpy(qexplanation, line);
break;
};
linecount++;
}
fclose(file);
return;
}
void write_question(int topic, char * qtype, char * qtext, char * qans, char *qexplanation){
char filename[100];
// topic == 1: "Threads";
// topic == 2: "Memory Management";
// topic == 3: "Scheduling";
// as operating with strings (char *) in C is pathetic
if(topic == 1) strcpy(filename,"Threads.txt");
else if(topic == 2) strcpy(filename,"Memory Management.txt");
else if(topic == 3) strcpy(filename,"Scheduling.txt");
int n_old_questions = n_questions(filename);
FILE *file = fopen(filename, "a");
fprintf(file, "\n%d\n", n_old_questions+1);
fprintf(file, "%s\n", qtype);
fprintf(file, "%s\n", qtext);
fprintf(file, "%s\n", qans);
fprintf(file, "%s\n", qexplanation);
fprintf(file, "question end\n");
fclose(file);
return;
}
//mauryaend
client_t *clients[MAX_CLIENTS];
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
void str_overwrite_stdout() {
printf("\r%s", "> ");
fflush(stdout);
}
void str_trim_lf (char* arr, int length) {
int i;
for (i = 0; i < length; i++) { // trim \n
if (arr[i] == '\n') {
arr[i] = '\0';
break;
}
}
}
void print_client_addr(struct sockaddr_in addr){
printf("%d.%d.%d.%d",
addr.sin_addr.s_addr & 0xff,
(addr.sin_addr.s_addr & 0xff00) >> 8,
(addr.sin_addr.s_addr & 0xff0000) >> 16,
(addr.sin_addr.s_addr & 0xff000000) >> 24);
}
/* Add clients to queue */
void queue_add(client_t *cl){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i < MAX_CLIENTS; ++i){
if(!clients[i]){
clients[i] = cl;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Remove clients to queue */
void queue_remove(int uid){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i < MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid == uid){
clients[i] = NULL;
break;
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
//zaid
void send_to(char *s, int uid){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i<MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid == uid){
if(write(clients[i]->sockfd, s, strlen(s)) < 0){
perror("ERROR: write to descriptor failed");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
//zaidend
/* Send message to all clients except sender */
void send_message(char *s, int uid){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i<MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid != uid){
if(write(clients[i]->sockfd, s, strlen(s)) < 0){
perror("ERROR: write to descriptor failed");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
void sendNames(int suid){
int csock;
pthread_mutex_lock(&clients_mutex);
for(int i=0; i<MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid == suid){
csock=clients[i]->sockfd;
}
}
}
char tempo[50];
for(int i=0; i<MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid != suid){
sprintf(tempo, "%s: %d", clients[i]->name, clients[i]->uid);
if(write(csock, tempo, strlen(tempo)) < 0){
perror("ERROR: write to descriptor failed");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Handle all communication with the client */
void *handle_client(void *arg){
char buff_out[BUFFER_SZ];
char name[32];
char mode[4];
char partner[32];
int partner_uid;
int leave_flag = 0;
cli_count++;
client_t *cli = (client_t *)arg;
// Name
if(recv(cli->sockfd, name, 32, 0) <= 0 || strlen(name) < 2 || strlen(name) >= 32-1){
printf("Didn't enter the name.\n");
leave_flag = 1;
} else{
strcpy(cli->name, name);
sprintf(buff_out, "%s has joined\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
}
if(recv(cli->sockfd, mode, 4, 0) <= 0){
printf("Didn't enter the mode.\n");
leave_flag = 1;
} else{
strcpy(cli->mode, mode);
}
/*if(mode[0]=='2'){
sendNames(cli->uid);
if(recv(cli->sockfd, partner, 32, 0) <= 0 || strlen(partner) < 2 || strlen(partner) >= 32-1){
printf("Didn't enter the partner.\n");
leave_flag = 1;
} else{
strcpy(cli->partner, partner);
}
}*/
bzero(buff_out, BUFFER_SZ);
//zaid
if(mode[0]=='2'){
while(1){
if (leave_flag) {
break;
}
send_to("Type userId of person you want to connect to: \n Availible userIds are:\n",cli->uid);
char buf1[32];
for(int i=10;i<cli_count+10;i++){
sprintf(buf1,"%d\n",i);
send_to(buf1,cli->uid);
sprintf(buf1,"Your userId is %d\n",cli->uid);
send_to(buf1,cli->uid);
}
int rv=recv(cli->sockfd, partner, BUFFER_SZ, 0);
partner_uid=atoi(partner);
sprintf(buf1,"Incoming connection from %d\n", cli->uid);
send_to(buf1,partner_uid);
send_to("Choose Topic:\n1:Threads\n2:Memory Management\n3:Scheduling\nEnter 'exit' to exit program\n",cli->uid);
send_to("Partner choosing topic please wait....\nEnter 'exit' to exit program\n",partner_uid);
int receive = recv(cli->sockfd, buff_out, BUFFER_SZ, 0);
if (receive > 0){
if(strlen(buff_out) > 0){
//zaid
if (buff_out[0]=='1')
{
get_question(1);
send_to(qtext,cli->uid);
send_to(qtext, partner_uid);
int receive = recv(cli->sockfd, ans_out, BUFFER_SZ, 0);
if(ans_out[0]==qans[0]) {
send_to("Right!!\n",cli->uid);
send_to(qexplanation,cli->uid);
send_to("Your partner choose Right!!\n",partner_uid);
send_to(qexplanation,partner_uid);
}
else{
send_to("Wrong:(\n",cli->uid);
send_to(qexplanation,cli->uid);
send_to("Your partner entered Wrong:(\n",partner_uid);
send_to(qexplanation,partner_uid);
}
}
else if (buff_out[0]=='2')
{
get_question(2);
send_to(qtext,cli->uid);
send_to(qtext, partner_uid);
int receive = recv(cli->sockfd, ans_out, BUFFER_SZ, 0);
if(ans_out[0]==qans[0]) {
send_to("Right!!\n",cli->uid);
send_to(qexplanation,cli->uid);
send_to("Your partner choose Right!!\n",partner_uid);
send_to(qexplanation,partner_uid);
}
else{
send_to("Wrong:(\n",cli->uid);
send_to(qexplanation,cli->uid);
send_to("Your partner entered Wrong:(\n",partner_uid);
send_to(qexplanation,partner_uid);
}
}
else if (buff_out[0]=='3')
{
get_question(3);
send_to(qtext,cli->uid);
send_to(qtext, partner_uid);
int receive = recv(cli->sockfd, ans_out, BUFFER_SZ, 0);
if(ans_out[0]==qans[0]) {
send_to("Right!!\n",cli->uid);
send_to(qexplanation,cli->uid);
send_to("Your partner choose Right!!\n",partner_uid);
send_to(qexplanation,partner_uid);
}
else{
send_to("Wrong:(\n",cli->uid);
send_to(qexplanation,cli->uid);
send_to("Your partner entered Wrong:(\n",partner_uid);
send_to(qexplanation,partner_uid);
}
}
//zaidend
send_message(buff_out, cli->uid);
str_trim_lf(buff_out, strlen(buff_out));
//if(buff_out[0]=='z') send_message("entered ind mlode", 1);
printf("%s: %s\n",cli->name, buff_out);
}
} else if (receive == 0 || strcmp(buff_out, "exit") == 0){
sprintf(buff_out, "%s has left\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
leave_flag = 1;
} else {
printf("ERROR: -1\n");
leave_flag = 1;
}
bzero(buff_out, BUFFER_SZ);
}
}
//zaidend
else if(mode[0]=='3'){
while(1){
if (leave_flag) {
break;
}
send_to("Choose Topic:\n1:Threads\n2:Memory Management\n3:Scheduling\nEnter 'exit' to exit program\n",cli->uid);
int receive = recv(cli->sockfd, buff_out, BUFFER_SZ, 0);
char qtype[32];
char qtext[2048];
char qans[32];
char qexplanation[2048];
if (receive > 0){
if(strlen(buff_out) > 0){
//zaid
if (buff_out[0]=='1')
{
send_to("Enter question type:\n FIB\n MCQ\n",cli->uid);
int receive1 = recv(cli->sockfd, qtype, 32, 0);
send_to("Enter question:\n",cli->uid);
int receive2 = recv(cli->sockfd, qtext, 2048, 0);
send_to("Enter question answer:\n",cli->uid);
int receive3 = recv(cli->sockfd, qans, 32, 0);
send_to("Enter question Explanation:\n",cli->uid);
int receive4 = recv(cli->sockfd, qexplanation, 2048, 0);
write_question(1,qtype,qtext, qans,qexplanation);
}
else if (buff_out[0]=='2')
{
send_to("Enter question type:\n FIB\n MCQ\n",cli->uid);
int receive1 = recv(cli->sockfd, qtype, 32, 0);
send_to("Enter question:\n",cli->uid);
int receive2 = recv(cli->sockfd, qtext, 2048, 0);
send_to("Enter question answer:\n",cli->uid);
int receive3 = recv(cli->sockfd, qans, 32, 0);
send_to("Enter question Explanation:\n",cli->uid);
int receive4 = recv(cli->sockfd, qexplanation, 2048, 0);
write_question(2,qtype,qtext, qans,qexplanation);
}
else if (buff_out[0]=='3')
{
send_to("Enter question type:\n FIB\n MCQ\n",cli->uid);
int receive1 = recv(cli->sockfd, qtype, 32, 0);
send_to("Enter question:\n",cli->uid);
int receive2 = recv(cli->sockfd, qtext, 2048, 0);
send_to("Enter question answer:\n",cli->uid);
int receive3 = recv(cli->sockfd, qans, 32, 0);
send_to("Enter question Explanation:\n",cli->uid);
int receive4 = recv(cli->sockfd, qexplanation, 2048, 0);
write_question(3,qtype,qtext, qans,qexplanation);
}
//zaidend
send_message(buff_out, cli->uid);
str_trim_lf(buff_out, strlen(buff_out));
//if(buff_out[0]=='z') send_message("entered ind mlode", 1);
printf("%s: %s\n",cli->name, buff_out);
}
} else if (receive == 0 || strcmp(buff_out, "exit") == 0){
sprintf(buff_out, "%s has left\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
leave_flag = 1;
} else {
printf("ERROR: -1\n");
leave_flag = 1;
}
bzero(buff_out, BUFFER_SZ);
}
}
else{
while(1){
if (leave_flag) {
break;
}
send_to("Choose Topic:\n1:Threads\n2:Memory Management\n3:Scheduling\nEnter 'exit' to exit program\n",cli->uid);
int receive = recv(cli->sockfd, buff_out, BUFFER_SZ, 0);
if (receive > 0){
if(strlen(buff_out) > 0){
//zaid
if (buff_out[0]=='1')
{
get_question(1);
send_to(qtext,cli->uid);
int receive = recv(cli->sockfd, ans_out, BUFFER_SZ, 0);
if(ans_out[0]==qans[0]) {
send_to("Right!!\n",cli->uid);
send_to(qexplanation,cli->uid);
}
else{
send_to("Wrong:(\n",cli->uid);
send_to(qexplanation,cli->uid);
}
}
else if (buff_out[0]=='2')
{
get_question(2);
send_to(qtext,cli->uid);
int receive = recv(cli->sockfd, ans_out, BUFFER_SZ, 0);
if(ans_out[0]==qans[0]) {
send_to("Right!!\n",cli->uid);
send_to(qexplanation,cli->uid);
}
else{
send_to("Wrong:(\n",cli->uid);
send_to(qexplanation,cli->uid);
}
}
else if (buff_out[0]=='3')
{
get_question(3);
send_to(qtext,cli->uid);
int receive = recv(cli->sockfd, ans_out, BUFFER_SZ, 0);
if(ans_out[0]==qans[0]) {
send_to("Right!!\n",cli->uid);
send_to(qexplanation,cli->uid);
}
else{
send_to("Wrong:(\n",cli->uid);
send_to(qexplanation,cli->uid);
}
}
//zaidend
send_message(buff_out, cli->uid);
str_trim_lf(buff_out, strlen(buff_out));
//if(buff_out[0]=='z') send_message("entered ind mlode", 1);
printf("%s: %s\n",cli->name, buff_out);
}
} else if (receive == 0 || strcmp(buff_out, "exit") == 0){
sprintf(buff_out, "%s has left\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
leave_flag = 1;
} else {
printf("ERROR: -1\n");
leave_flag = 1;
}
bzero(buff_out, BUFFER_SZ);
}
}
/* Delete client from queue and yield thread */
close(cli->sockfd);
queue_remove(cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char **argv){
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int option = 1;
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
/* Socket settings */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(ip);
serv_addr.sin_port = htons(port);
/* Ignore pipe signals */
signal(SIGPIPE, SIG_IGN);
if(setsockopt(listenfd, SOL_SOCKET,(SO_REUSEPORT | SO_REUSEADDR),(char*)&option,sizeof(option)) < 0){
perror("ERROR: setsockopt failed");
return EXIT_FAILURE;
}
/* Bind */
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR: Socket binding failed");
return EXIT_FAILURE;
}
/* Listen */
if (listen(listenfd, 10) < 0) {
perror("ERROR: Socket listening failed");
return EXIT_FAILURE;
}
printf("=== WELCOME TO THE QuizServer ===\n");
while(1){
socklen_t clilen = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
/* Check if max clients is reached */
if((cli_count + 1) == MAX_CLIENTS){
printf("Max clients reached. Rejected: ");
print_client_addr(cli_addr);
printf(":%d\n", cli_addr.sin_port);
close(connfd);
continue;
}
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->address = cli_addr;
cli->sockfd = connfd;
cli->uid = uid++;
/* Add client to the queue and fork thread */
queue_add(cli);
pthread_create(&tid, NULL, &handle_client, (void*)cli);
/* Reduce CPU usage */
sleep(1);
}
return EXIT_SUCCESS;
}