From ace4e3c8e5709a32d567be459c48d4cd3414f70b Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Tue, 11 Apr 2023 08:09:27 -0500 Subject: [PATCH] Added code to retrieve sender (by calling the appropriate message_helper function), started refactoring code so that all data is processed in one shot, instead of waiting for multiple 'recv's --- main.c | 83 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index f29b22e..bd99e0f 100644 --- a/main.c +++ b/main.c @@ -14,6 +14,7 @@ #define BUFFER_SIZE 10000 #define MAX_CONNECTIONS 100 +#define DATA_SIZE 50000 User** create_user_list(char* filename); void sigint_handler(int dummy); @@ -22,9 +23,10 @@ User* fetch_user(char* username); User** users; int num_users; -bool stop_running = false; +bool stop_running; int main() { + stop_running = false; signal(SIGINT,sigint_handler); @@ -34,12 +36,14 @@ int main() { fd_set read_fd_set; int conn_sockets[MAX_CONNECTIONS] = {-1}; User* to_user[MAX_CONNECTIONS] = {NULL}; + User* from_user[MAX_CONNECTIONS] = {NULL}; FD_ZERO(&read_fd_set); char buffer[BUFFER_SIZE]; + char data[DATA_SIZE]; num_users = num_of_lines("user_file.txt"); users = create_user_list("user_file.txt"); - + struct sockaddr addr_struct; int server_sock = create_local(4,'T',"127.0.0.1",30000,&addr_struct); conn_sockets[0] = server_sock; @@ -69,43 +73,70 @@ int main() { for (int i=1; i < MAX_CONNECTIONS; i++) { if (FD_ISSET(conn_sockets[i],&read_fd_set)) { - int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0); - if (num_bytes_read <= 0) { - close(conn_sockets[i]); - conn_sockets[i] = 0; - to_user[i] = NULL; - } else { - if (to_user[i] == NULL) { - to_user[i] = fetch_user(fetch_dest_user_string(buffer)); + do { + + while ( strcmp(buffer,"END_OF_DATA\r\n") != 0 ) { + int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0); + if (num_bytes_read <= 0) { + close(conn_sockets[i]); + conn_sockets[i] = 0; + to_user[i] = NULL; + GOTO continue_for_loop; + } + strcat(data, buffer); + } + + strcat(data,"\0"); + + if (num_bytes_read <= 0) { + close(conn_sockets[i]); + conn_sockets[i] = 0; + to_user[i] = NULL; + break; + } else { + + if (from_user[i] == NULL) { + from_user[i] = fetch_user(fetch_sender_user_string(buffer)); + if (from_user[i] == NULL) { + printf("Please identify yourself.\n"); + raise(SIGINT); + return 150; + } else { + printf("You are %s\n",from_user[i]->username); + } + continue; + } + if (to_user[i] == NULL) { - printf("Invalid message format or User\n"); - exit(241); - } else { - printf("Message intended for %s\n",to_user[i]->username); - exit(0); + to_user[i] = fetch_user(fetch_dest_user_string(buffer)); + if (to_user[i] == NULL) { + printf("Invalid message format or User\n"); + raise(SIGINT); + } else { + printf("Message intended for %s\n",to_user[i]->username); + } + continue; } + char* message = fetch_message_string(buffer); + } - -// for (int i=0;i= 0; i--) { + close(i); close(conn_sockets[i]); } - exit(130); + return 130; }