-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
755 lines (613 loc) · 22.9 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// gcc server1.c -o serv.out
// gcc client1.c -o cli.out
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <pthread.h>
#include <asm-generic/socket.h>
#include <sys/stat.h>
#include <time.h>
#define PORT 8080
#define BUFFER_SIZE 1024
// User structure to hold user information
typedef struct{
int userid; // User's ID
char phone[20]; // User's phone number
char name[20]; // User's first name
char surname[20]; // User's surname
} user;
// Function prototypes
void receive_message(int client_sockfd, char* buffer);
int send_message(int sockfd, char* message);
void getCurrentDateTime(char* dateTimeString, int maxLength);
void writeStructToFile(char* filename, user* data, int numEntries);
user* readStructFromFile(char* filename, size_t* numEntries);
void sendContact(int sockid);
void initializeFileSystem(int userid);
void logUser(int sockid, int userid);
void addContact(int sockid, int userid);
void deleteContact(int sockid, int userid);
void listContacts(int sockid);
void checkContact(int sockid, user source, int destid);
void appendMessage(const char *filename, const char *message);
void sendMessage(int sockid, int userid);
int getMessages(char* messageList, char* filename, int* chatIDs);
void checkMessages(int sockid, int userid);
void *handle_client(void *arg);
// Main function: Entry point of the program
int main() {
int server_fd, new_socket; // File descriptors for server and new socket
struct sockaddr_in address; // Structure for the server address
int opt = 1; // Socket option
socklen_t addrlen = sizeof(address); // Length of the address
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed"); // Socket creation failed
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt"); // Setting socket options failed
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET; // Internet Protocol
address.sin_addr.s_addr = INADDR_ANY; // Any incoming interface
address.sin_port = htons(PORT); // Port number
// Binding the socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed"); // Bind failed
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen"); // Listen failed
exit(EXIT_FAILURE);
}
printf("Server successfully started on port %d...\n", PORT);
// Main loop to accept incoming connections
while (1) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, &addrlen)) < 0) {
perror("accept"); // Accept failed
exit(EXIT_FAILURE);
}
// Create a new thread for each client
pthread_t thread_id;
if (pthread_create(&thread_id, NULL, handle_client, (void *)&new_socket) != 0) {
perror("pthread_create"); // Thread creation failed
exit(EXIT_FAILURE);
}
// Detach the thread to avoid memory leaks
pthread_detach(thread_id);
}
// Closing the listening socket (unreachable in this code)
close(server_fd);
return 0;
}
void receive_message(int client_sockfd, char* buffer) {
memset(buffer, '\0', BUFFER_SIZE);
read(client_sockfd, buffer, BUFFER_SIZE - 1);
//printf("Client Side: %s\n", buffer);
}
int send_message(int sockfd, char* message) {
//printf("This Side: %s\n", message);
if(strlen(message) >= 1)
send(sockfd, message, strlen(message), 0);
}
void displayContact(user* data, int numEntries) {
int i;
printf("| %-10s| %-20s| %-20s| %-20s\n", "User ID", "Phone", "Name", "Surname");
printf("|──────────────────────────────────────────────────────\n");
for (i = 0; i < numEntries; ++i) {
printf("| %-10d| %-20s| %-20s| %-20s\n", data[i].userid, data[i].phone, data[i].name, data[i].surname);
if (i < numEntries - 1) {
printf("|───────────|─────────────────────|────────────────────\n");
}
}
printf("|──────────────────────────────────────────────────────\n");
}
void getCurrentDateTime(char* dateTimeString, int maxLength) {
time_t t;
struct tm* tmInfo;
// O anki zamanı al
time(&t);
tmInfo = localtime(&t);
// Tarih ve saat bilgilerini biçimlendirip dateTimeString'e yerleştir
snprintf(dateTimeString, maxLength, "%02d:%02d %02d-%02d-%04d",
tmInfo->tm_hour, tmInfo->tm_min,
tmInfo->tm_mday, tmInfo->tm_mon + 1, tmInfo->tm_year + 1900);
}
void writeStructToFile(char* filename, user* data, int numEntries) {
FILE* file = fopen(filename, "w"); // Open the file for writing (overwrite mode)
int i;
if (file != NULL) {
// Write the counter indicator
fprintf(file, "%d\n", numEntries);
// Write each user to the file
for (i = 0; i < numEntries; ++i) {
int result = fprintf(file, "%d, %s, %s, %s;\n", data[i].userid, data[i].phone, data[i].name, data[i].surname);
// Check if writing was successful
if (result < 0) {
fprintf(stderr, "Error writing data to file.\n");
break; // Exit loop if an error occurs
}
}
fclose(file);
} else {
fprintf(stderr, "File Opening Error\n");
}
}
user* readStructFromFile(char* filename, size_t* numEntries) {
FILE* file = fopen(filename, "r"); // Open the file for reading in text mode
int i;
if (file != NULL) {
// Read the counter indicator
if (fscanf(file, "%lu", numEntries) != 1) {
// Error handling for invalid or missing counter indicator
fclose(file);
return NULL;
}
// Skip the newline character after the counter indicator
fgetc(file);
// Allocate memory for the users
user* data = (user*)malloc(*numEntries * sizeof(user));
// Read each user from the file
for (i = 0; i < *numEntries; ++i) {
fscanf(file, "%d, %19[^,], %19[^,], %19[^;\n]%*[; \t\n]",
&data[i].userid, data[i].phone, data[i].name, data[i].surname);
}
fclose(file);
return data;
} else {
*numEntries = 0;
return NULL;
}
}
void sendContact(int sockid){
char buffer[BUFFER_SIZE];
receive_message(sockid, buffer);
char* user_id_str = buffer + strlen("/recvContact");
int userid = atoi(user_id_str);
char filename[40];
if(userid != -1){
sprintf(filename, "data/%d/contacts.txt", userid);
}
else{
sprintf(filename, "users.txt");
}
size_t numEntries = 0;
user* data = readStructFromFile(filename, &numEntries);
printf("sending contacts\n");
if(numEntries == 0){
send_message(sockid, "Kayitli Kullanici Yok.");
return;
}
else{
snprintf(buffer, BUFFER_SIZE, "%d", (int)numEntries);
send_message(sockid, buffer);
printf("%s\n", buffer);
}
int i;
receive_message(sockid, buffer);
if(strcmp(buffer, "/ready") == 0){
for(i=0; i<numEntries; i++){
char buffer2[BUFFER_SIZE];
memset(buffer, '\0', BUFFER_SIZE);
memset(buffer2, '\0', BUFFER_SIZE);
snprintf(buffer, BUFFER_SIZE, "%s, %s, %s, %d", data[i].name, data[i].surname, data[i].phone, data[i].userid);
printf("%s, %s, %s, %d\n", data[i].name, data[i].surname, data[i].phone, data[i].userid);
do{
send_message(sockid, buffer);
receive_message(sockid, buffer2);
}while(strcmp(buffer2, "received") != 0);
//printf("%s\n", buffer);
}
}
return;
}
void initializeFileSystem(int userid) {
// Create user directory
char data_directory[] = "data";
struct stat st_data = {0};
if (stat(data_directory, &st_data) == -1) {
if (mkdir(data_directory, 0777) != 0) {
fprintf(stderr, "Error creating data directory\n");
// Handle error as needed
}
}
// Check if the directory already exists, if not, create it
char user_directory[10];
sprintf(user_directory, "data/%d", userid);
struct stat st = {0};
if (stat(user_directory, &st) == -1) {
if (mkdir(user_directory, 0777) != 0) {
fprintf(stderr, "Error creating user directory for user_id %d\n", userid);
// Handle error as needed
}
}
// Create messages directory
char messages_directory[40];
sprintf(messages_directory, "%s/messages", user_directory);
if (stat(messages_directory, &st) == -1) {
if (mkdir(messages_directory, 0777) != 0) {
fprintf(stderr, "Error creating messages directory for userid %d\n", userid);
// Handle error as needed
}
}
// Create contacts file
char contacts_file[40];
sprintf(contacts_file, "%s/contacts.txt", user_directory);
FILE* file = fopen(contacts_file, "a"); // Open for append
if (file == NULL) {
fprintf(stderr, "Error creating contacts file for userid %d\n", userid);
// Handle error as needed
}
fclose(file);
}
void logUser(int sockid, int userid) {
size_t numEntries = 0;
user* data = readStructFromFile("users.txt", &numEntries);
char buffer[BUFFER_SIZE];
// Check if there is any user with this ID
int found = 0;
int i = 0;
if (data != NULL) {
while (found == 0 && i < numEntries) {
if (data[i].userid == userid) {
// User found, send user info to the client
char response[BUFFER_SIZE];
snprintf(response, BUFFER_SIZE, "%s, %s, %s, %d", data[i].name, data[i].surname, data[i].phone, data[i].userid);
send_message(sockid, response);
found = 1;
printf("Logged in %d\n", userid);
}
i++;
}
}
// If the user is not found, ask the client to register
if (!found) {
send_message(sockid, "/register");
memset(buffer, '\0', BUFFER_SIZE);
receive_message(sockid, buffer);
// Reallocate memory for one more user
user* temp = realloc(data, (numEntries + 1) * sizeof(user));
if (temp == NULL) {
fprintf(stderr, "Error reallocating memory.\n");
free(data); // Free original data before exiting
exit(EXIT_FAILURE);
}
data = temp;
sscanf(buffer, "%[^,], %[^,], %[^,], %d", data[numEntries].name, data[numEntries].surname, data[numEntries].phone, &data[numEntries].userid);
// Update the number of entries
numEntries++;
// Save the updated user data to the file
writeStructToFile("users.txt", data, numEntries);
initializeFileSystem(userid);
char response[BUFFER_SIZE];
snprintf(response, sizeof(response), "%d, %s, %s, %s",
data[numEntries - 1].userid, data[numEntries - 1].name, data[numEntries - 1].surname, data[numEntries - 1].phone);
send_message(sockid, response);
printf("Registered %d\n", userid);
}
// Free allocated memory for user data
free(data);
}
void addContact(int sockid, int userid){
char buffer[BUFFER_SIZE];
send_message(sockid, "/ok");
sendContact(sockid);
do{
receive_message(sockid, buffer);
}while (strlen(buffer) == 0);
int addUser = atoi(buffer);
// if client wants to add himself
if(addUser == userid){
send_message(sockid, "You can't add yourself\n");
return;
}
char filename[40];
sprintf(filename, "data/%d/contacts.txt", userid);
size_t totalUsers = 0, numEntries = 0;
int i=0, found=0;
user* contactList = readStructFromFile("users.txt", &totalUsers);
user* data = readStructFromFile(filename, &numEntries);
user newUser;
while(data != NULL && i < totalUsers){
if(data[i].userid == addUser){
send_message(sockid, "User already exists\n");
return;
}
i++;
}
i=0;
while(found == 0 && i < totalUsers){
if(data != NULL && data[i].userid == addUser){
return;
}
if(contactList[i].userid == addUser){
found = 1;
user* temp = realloc(data, (numEntries + 1) * sizeof(user));
if (temp == NULL) {
fprintf(stderr, "Error reallocating memory.\n");
free(data); // Free original data before exiting
exit(EXIT_FAILURE);
}
data = temp;
strcpy(data[numEntries].name, contactList[i].name);
strcpy(data[numEntries].surname, contactList[i].surname);
strcpy(data[numEntries].phone, contactList[i].phone);
data[numEntries].userid = addUser;
printf("%s, %s, %s, %d\n", data[numEntries].name, data[numEntries].surname, data[numEntries].phone, data[numEntries].userid);
// Update the number of entries
numEntries++;
//displayContact(data, numEntries);
// Save the updated user data to the file
writeStructToFile(filename, data, numEntries);
}
i++;
}
if(found == 0)
send_message(sockid, "Failed to add\n");
else
send_message(sockid, "Added to contact\n");
return;
}
void deleteContact(int sockid, int userid){
char buffer[BUFFER_SIZE];
send_message(sockid, "/ok");
sendContact(sockid);
do{
receive_message(sockid, buffer);
}while (strlen(buffer) == 0);
if(strcmp(buffer, "/empty") == 0)
return;
int addUser = atoi(buffer);
// if client wants to add himself
if(addUser == userid){
send_message(sockid, "You can't delete yourself\n");
return;
}
char filename[40];
sprintf(filename, "data/%d/contacts.txt", userid);
size_t numEntries = 0;
int i=0;
user* data = readStructFromFile(filename, &numEntries);
user newUser;
while(data != NULL && i < numEntries){
if(data[i].userid == addUser){
int j;
numEntries--;
if(numEntries != 0){
for(j = i; j<numEntries; j++){
strcpy(data[j].name, data[j+1].name);
strcpy(data[j].surname, data[j+1].surname);
strcpy(data[j].phone, data[j+1].phone);
data[j].userid = data[j+1].userid;
}
user* temp = realloc(data, (numEntries) * sizeof(user));
if (temp == NULL) {
fprintf(stderr, "Error reallocating memory.\n");
free(data); // Free original data before exiting
exit(EXIT_FAILURE);
}
data = temp;
writeStructToFile(filename, data, numEntries);
}
else{
FILE* file = fopen(filename, "w"); // Open the file for writing (overwrite mode)
if (file != NULL)
// Write the counter indicator
fprintf(file, "%s", "");
}
send_message(sockid, "Kullanici Silindi.\n");
return;
}
i++;
}
send_message(sockid, "Kullanici Bulunamadi.\n");
}
void listContacts(int sockid){
char buffer[BUFFER_SIZE];
send_message(sockid, "/ok");
sendContact(sockid);
do {
receive_message(sockid, buffer);
}while(strcmp(buffer, "/done"));
}
void checkContact(int sockid, user source, int destid){
char filename[20];
sprintf(filename, "data/%d/contacts.txt", destid);
size_t numEntries = 0;
user* data = readStructFromFile(filename, &numEntries);
int i, found;
user newUser;
while(data != NULL && i < numEntries){
if(data[i].userid == source.userid){
send_message(sockid, "Message Sent");
return;
}
i++;
}
user* temp = realloc(data, (numEntries + 1) * sizeof(user));
if (temp == NULL) {
fprintf(stderr, "Error reallocating memory.\n");
free(data); // Free original data before exiting
exit(EXIT_FAILURE);
}
data = temp;
strcpy(data[numEntries].name, source.name);
strcpy(data[numEntries].surname, source.surname);
strcpy(data[numEntries].phone, source.phone);
data[numEntries].userid = source.userid;
printf("%s, %s, %s, %d\n", data[numEntries].name, data[numEntries].surname, data[numEntries].phone, data[numEntries].userid);
// Update the number of entries
numEntries++;
// Save the updated user data to the file
writeStructToFile(filename, data, numEntries);
send_message(sockid, "You've added to destination's contact list \nMessage Sent");
return;
}
void appendMessage(const char *filename, const char *message) {
FILE* file = fopen(filename, "a"); // Open file in append mode
if (file == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
fprintf(file, "%s\n", message); // Append the message to the file
fclose(file); // Close the file
}
void sendMessage(int sockid, int userid){
char buffer[BUFFER_SIZE];
send_message(sockid, "/ok");
sendContact(sockid);
receive_message(sockid, buffer);
if(strcmp(buffer, "/emptyContact") == 0){
return;
}
else{
int i;
size_t numEntries;
char message[BUFFER_SIZE-100];
int destid;
char filename[10];
char msg_directory[60];
char date[20];
char sourceMsg[BUFFER_SIZE];
char destMsg[BUFFER_SIZE];
sscanf(buffer, "%d, %[^\n]", &destid, message);
user* data = readStructFromFile("users.txt", &numEntries);
user destUser, sourceUser;
for (i = 0; i < numEntries; i++){
if(data[i].userid == userid){
sourceUser = data[i];
}
else if(data[i].userid == destid){
destUser = data[i];
}
}
getCurrentDateTime(date, 20);
// mesaj atan kullanıcının dosyasına sen ile başlayacak şekilde
// yerleştirir.
sprintf(sourceMsg, "%s | %-15s: %s", date, "Sen", message);
sprintf(msg_directory, "data/%d/messages/%d.txt", userid, destid);
appendMessage(msg_directory, sourceMsg);
sprintf(destMsg, "%s | %-15s: %s", date, sourceUser.name, message);
sprintf(msg_directory, "data/%d/messages/%d.txt", destid, userid);
appendMessage(msg_directory, destMsg);
sprintf(destMsg, "%s %s %d", sourceUser.name, sourceUser.surname, sourceUser.userid);
sprintf(msg_directory, "data/%d/messages/messages.txt", destid);
appendMessage(msg_directory, destMsg);
// check if destination added source
checkContact(sockid, sourceUser, destUser.userid);
}
}
int getMessages(char* messageList, char* filename, int* chatIDs) {
FILE* file = fopen(filename, "r+");
if (file == NULL) {
printf("get message\n");
perror("Error opening file");
return 1;
}
int lineNumber = 1;
char dummy1[200];
char dummy2[100];
memset(dummy2, '\0', 100);
memset(dummy1, '\0', 200);
// Assuming each line in the file is a message
while (fgets(dummy2, 100, file) != NULL) {
// Add line number to the messageList
chatIDs[lineNumber-1] = atoi(&dummy2[strlen(dummy2)-2]);
sprintf(dummy1, "%d - %s", lineNumber, dummy2);
strcat(messageList, dummy1);
memset(dummy2, '\0', 200);
lineNumber++;
}
fclose(file);
return 0;
}
void checkMessages(int sockid, int userid){
char buffer[BUFFER_SIZE];
int status = 1;
send_message(sockid, "/ok");
char messageList[BUFFER_SIZE];
memset(messageList, '\0', BUFFER_SIZE);
char filename[40];
int chatIDs[40];
sprintf(filename, "data/%d/messages/messages.txt", userid);
status = getMessages(messageList, filename, chatIDs);
do{
receive_message(sockid, buffer);
}while (strcmp(buffer, "/ok") != 0);
if(status){
send_message(sockid, "/noFile");
return;
}
send_message(sockid, messageList);
do{
receive_message(sockid, buffer);
}while (strcmp(buffer, "") == 0);
int chatid = atoi(buffer);
printf("ID: %d\n", chatid);
sprintf(filename, "data/%d/messages/%d.txt", userid, chatid);
FILE* file = fopen(filename, "r+");
char dummy[200];
memset(dummy, '\0', 200);
do{
receive_message(sockid, buffer);
}while (strcmp(buffer, "/start") != 0);
printf("Got start\n");
// Assuming each line in the file is a message
while (fgets(dummy, 100, file) != NULL) {
// Add line number to the messageList
printf("%s", dummy);
send_message(sockid, dummy);
receive_message(sockid, buffer);
}
send_message(sockid, "/EOF");
fclose(file);
}
void *handle_client(void *arg) {
int sockid = *((int *)arg);
char buffer[BUFFER_SIZE] = {0};
int status = 1;
receive_message(sockid, buffer);
const char* user_id_str = buffer + strlen("/login"); // Skip the "/login" part
int user_id = atoi(user_id_str);
// Now you have the user ID, you can use it as needed
printf("User %d is trying to log in.\n", user_id);
logUser(sockid, user_id);
printf("Login succes\n");
while (status) {
receive_message(sockid, buffer);
if (strncmp(buffer, "/exit", strlen("/exit")) == 0){
const char* user_id_str = buffer + strlen("/exit"); // Skip the "/exit" part
int user_id = atoi(user_id_str);
printf("User %d is logged out\n", user_id);
status = 0;
}
else if (strcmp(buffer, "/addContact") == 0){
addContact(sockid, user_id);
}
else if (strcmp(buffer, "/deleteContact") == 0){
deleteContact(sockid, user_id);
}
else if (strcmp(buffer, "/listContacts") == 0){
listContacts(sockid);
}
else if (strcmp(buffer, "/sendMessages") == 0){
sendMessage(sockid, user_id);
}
else if (strcmp(buffer, "/checkMessages") == 0){
checkMessages(sockid, user_id);
}
else if (strcmp(buffer, "/deleteMessage") == 0){
}
else if(strcmp(buffer, "") != 0)
printf("%s\n", buffer);
}
// Close the socket and exit the thread
close(sockid);
pthread_exit(NULL);
return NULL;
}