diff --git a/main.c b/main.c index 61de6be..43d0181 100644 --- a/main.c +++ b/main.c @@ -94,7 +94,16 @@ int main() { } strcat(data,"\0"); - if (strcmp(strtok(data," \r\n"), "SENDING") == 0) { + buffer[0] = '\0'; /* Clear the buffer */ + + char* send_or_receive = malloc(10); + int k=0; + while (*(data + k) != '\n') { + k++; + } + memcpy(send_or_receive,data,k); + + if (strstr(send_or_receive, "SENDING") != NULL) { if (from_user[i] == NULL) { from_user[i] = fetch_user(fetch_from_string(data,"IAM")); if (from_user[i] == NULL) { @@ -123,7 +132,7 @@ int main() { return -10; } - printf("Your message is: %s\n",message_string); + printf("Your message is: \n------------------\n%s\n------------------\n",message_string); if (user_to_index(to_user[i]) < 0) { printf("Recipient user does not exist!\n"); @@ -135,7 +144,7 @@ int main() { printf("Message has been pushed onto stack for %s\n",to_user[i]->username); - } else if (strcmp(strtok(buffer," \r\n"), "SENDING") == 0) { + } else if (strstr(send_or_receive, "RECEIVING") != NULL) { if (from_user[i] == NULL) { from_user[i] = fetch_user(fetch_from_string(data,"IAM")); @@ -155,7 +164,7 @@ int main() { printf("Invalid GET request.\n"); return -30; } else { - if (strstr(num_of_messages_string,"ALL") == 0) { + if (strstr(num_of_messages_string,"ALL") != NULL) { num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]); } else { num_of_messages = atoi(num_of_messages_string); @@ -166,7 +175,7 @@ int main() { num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]); } - for (int i=0;itext); } - } else { printf("You are not sending or receiving.\n"); } - - close(conn_sockets[i]); - + close(conn_sockets[i]); + conn_sockets[i] = -1; + + continue_for_loop: + memset(data,0,DATA_SIZE); + memset(buffer,0,BUFFER_SIZE); + to_user[i] = NULL; + from_user[i] = NULL; } - - - continue_for_loop: - data[0] = '\0'; /* This effectively clears the string, since the first element is null. */ - to_user[i] = NULL; } - } - if (stop_running == true) { - printf("Stopping...\n"); - for (int i=MAX_CONNECTIONS-1; i>= 0; i--) { - close(i); - close(conn_sockets[i]); + if (stop_running == true) { + printf("Stopping...\n"); + for (int i=MAX_CONNECTIONS-1; i>= 0; i--) { + close(i); + close(conn_sockets[i]); + } + return 130; + } + } - return 130; - } } - -// Message message = new_message("Hello, this is a text message",users[0],users[1]); - - - -// printf("Message was: %s\nSentfrom: %s\nSent to: %s\nWith password: %s\nSent at: %s\n",message.text, message.sender.username, message.recipient.username, message.recipient.password, asctime(&message.timeinfo)); - } @@ -242,7 +244,9 @@ User** create_user_list(char* filename) { } void sigint_handler(int dummy) { - stop_running = true; + stop_running = true; + printf("sigint_handler is stopping...\n"); + exit(130); } User* fetch_user(char* username) {