diff --git a/src/server.c b/src/server.c index b44e7d3..0e054fd 100644 --- a/src/server.c +++ b/src/server.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -15,6 +16,8 @@ int main(void) { int serv_sockid, client_sockid; struct sockaddr_in server_socket; struct sockaddr_in client_socket; + pthread_t clientlistener, clientsender; + void *retval; pid_t pid; long addr_len; @@ -70,18 +73,17 @@ int main(void) { } else { printf("received connection from %s\n", ip4tostring(ntohl(client_socket.sin_addr.s_addr))); - /* Create a dedicated process for the client */ - if ((pid = fork()) == -1) { - perror("fork"); - write(client_sockid, "server error\n", 14); - } else { - if (pid == 0) { - service(client_sockid); - return EXIT_SUCCESS; - } + if (pthread_create(&clientsender, NULL, ClientSender, NULL) != 0) { + fprintf(stderr, "pthread_create: error for ClientSender\n"); + exit(EXIT_FAILURE); } - /* Let the child handle the file descriptor that was just created */ - close(client_sockid); + if (pthread_create(&clientlistener, NULL, ClientListener, NULL) != 0) { + fprintf(stderr, "pthread_create: error for ClientSender\n"); + exit(EXIT_FAILURE); + } + + (void) pthread_join(clientsender, &retval); + (void) pthread_join(clientlistener, &retval); } } @@ -89,10 +91,12 @@ int main(void) { } /* Thread, parses and handles messages */ -void Dispatcher(void) {} +void *Dispatcher(void *arg) { + pthread_exit(0); +} /* Thread, listens for incoming messages */ -void ClientListener(int client_sockid) { +void *ClientListener(void *arg) { char buf[BUF_LEN]; int n = 0; @@ -106,10 +110,14 @@ void ClientListener(int client_sockid) { break; } } + + pthread_exit(0); } /* Thread, sends messages to the client */ -void ClientSender(int message_pipe, int client_sockid) {} +void *ClientSender(void *arg) { + pthread_exit(0); +} /* Handles a client after its connection */ // TODO: remove this function diff --git a/src/server.h b/src/server.h index 2b29758..e29799e 100644 --- a/src/server.h +++ b/src/server.h @@ -4,13 +4,13 @@ #define LISTEN_ALL // define to listen on 0.0.0.0, else loopback /* Thread, parses and handles messages */ -void Dispatcher(void); +void *Dispatcher(void*); /* Thread, listens for incoming messages */ -void ClientListener(int); +void *ClientListener(void*); /* Thread, sends messages to the client */ -void ClientSender(int, int); +void *ClientSender(void*); /* Handles a client after its connection */ void service(int);