From 44f2df363a67372465c6e3642569bc4416a2b8a0 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@clevo" Date: Fri, 21 Oct 2022 16:57:32 +0200 Subject: [PATCH] Create Dispatcher thread --- src/server.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index 9ada393..02041cf 100644 --- a/src/server.c +++ b/src/server.c @@ -22,7 +22,7 @@ int main(void) { int message_pipe[2]; struct sockaddr_in server_socket; struct sockaddr_in client_socket; - pthread_t clientlistener, clientsender; + pthread_t dispatcher, clientlistener, clientsender; void *retval; pid_t pid; long addr_len; @@ -80,6 +80,11 @@ int main(void) { return EXIT_FAILURE; } + if (pthread_create(&dispatcher, NULL, Dispatcher, NULL) != 0) { + fprintf(stderr, "pthread_create: error for Dispatcher\n"); + exit(EXIT_FAILURE); + } + /* Loop to accept incoming connections */ while (1) { if ((client_sockid = accept(serv_sockid, (struct sockaddr *) &client_socket, (socklen_t *) &addr_len)) < 0) { @@ -116,12 +121,14 @@ int main(void) { exit(EXIT_FAILURE); } + // TODO: joining here prevents creating other clients, need to move the joins to some client_disconnect function (void) pthread_join(clientsender, &retval); (void) pthread_join(clientlistener, &retval); } } printf("Server end, closing connection\n"); + (void) pthread_join(dispatcher, &retval); close(serv_sockid); return EXIT_SUCCESS; }