Get client id before creating pipe: if client refused, no pipe created

This commit is contained in:
Tunui Franken 2022-10-25 08:42:48 +02:00
parent d80cc79e8f
commit 39fde32fd6

View file

@ -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");