From 39fde32fd6d3d1790e25c705b032ca60c7405673 Mon Sep 17 00:00:00 2001 From: Tunui Franken Date: Tue, 25 Oct 2022 08:42:48 +0200 Subject: [PATCH] Get client id before creating pipe: if client refused, no pipe created --- src/server.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/server.c b/src/server.c index 060fc45..882767c 100644 --- a/src/server.c +++ b/src/server.c @@ -93,13 +93,7 @@ int main(int argc, char *argv[]) { } else { printf("received connection from %s\n", ip4tostring(ntohl(client_socket.sin_addr.s_addr))); - /* Create a pipe for message exchange between Dispatcher and ClientSender */ - if (pipe(message_pipe) != 0) { - perror("pipe"); - return EXIT_FAILURE; - } - - /* Create client details */ + /* Get client id to make sure we can handle him */ if ((client_id = get_next_free_client_id()) == -1) { /* Client array is full, refuse connection */ fprintf(stderr, "get_next_free_client_id: Client array must be full\n"); @@ -109,6 +103,12 @@ int main(int argc, char *argv[]) { continue; } + /* Create a pipe for message exchange between Dispatcher and ClientSender */ + if (pipe(message_pipe) != 0) { + perror("pipe"); + return EXIT_FAILURE; + } + /* Start client threads */ if (pthread_create(&clientsender, NULL, ClientSender, &clients[client_id]) != 0) { fprintf(stderr, "pthread_create: error for ClientSender\n");