From 97e2d1d09781e3526a463ccc07b9bce3eaa3c7a0 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Tue, 21 Feb 2023 19:34:40 +0100 Subject: [PATCH] moved accept-loop to why2_accept_thread --- include/chat/misc.h | 1 + src/chat/main/server.c | 11 +++-------- src/chat/misc.c | 23 ++++++++++++++++++----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/include/chat/misc.h b/include/chat/misc.h index 2eec6a5..011895f 100644 --- a/include/chat/misc.h +++ b/include/chat/misc.h @@ -23,5 +23,6 @@ void why2_send_socket(char *text, int socket); //send socket.... wtf did you exp char *why2_read_socket(int socket); //read lol void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD void why2_register_connection(int socket); //ADD SOCKET TO LIST +void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS #endif \ No newline at end of file diff --git a/src/chat/main/server.c b/src/chat/main/server.c index cc89441..1fb7122 100644 --- a/src/chat/main/server.c +++ b/src/chat/main/server.c @@ -23,7 +23,6 @@ along with this program. If not, see . int main(void) { int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET - int accepted; pthread_t thread; if (listen_socket < 0) why2_die("Failed creating socket."); @@ -42,15 +41,11 @@ int main(void) printf("Server enabled.\n\n"); - //LOOP ACCEPT - for (;;) - { - accepted = accept(listen_socket, (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one) + pthread_create(&thread, NULL, why2_accept_thread, NULL); - if (accepted == -1) continue; + //TODO: Add getline() - pthread_create(&thread, NULL, why2_communicate_thread, &accepted); - } + pthread_join(thread, NULL); return 0; } \ No newline at end of file diff --git a/src/chat/misc.c b/src/chat/misc.c index 800b404..734a99e 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -18,13 +18,10 @@ along with this program. If not, see . #include -#include -#include -#include -#include -#include #include +#include + #include //LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) @@ -191,4 +188,20 @@ char *why2_read_socket(int socket) void why2_register_connection(int socket) { push_to_list(socket); //SEND +} + +void *why2_accept_thread(void *socket) +{ + int accepted; + pthread_t thread; + + //LOOP ACCEPT + for (;;) + { + accepted = accept(*((int*) socket), (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one) + + if (accepted == -1) continue; + + pthread_create(&thread, NULL, why2_communicate_thread, &accepted); + } } \ No newline at end of file