diff --git a/Assignment-06/starter/rsh_server.c b/Assignment-06/starter/rsh_server.c index f10127fe0859d26a198b1d42a664651998d78c10..e4aa7203ec0699a90590d3ebfc9cdad1c39417eb 100644 --- a/Assignment-06/starter/rsh_server.c +++ b/Assignment-06/starter/rsh_server.c @@ -8,6 +8,7 @@ #include <sys/un.h> #include <fcntl.h> #include <errno.h> +#include <pthread.h> //INCLUDES for extra credit //#include <signal.h> @@ -47,7 +48,7 @@ * TO DO SOMETHING WITH THE is_threaded ARGUMENT HOWEVER. */ - int start_server(char *ifaces, int port, int is_threaded) { +int start_server(char *ifaces, int port, int is_threaded) { (void)is_threaded; int svr_socket; int rc; @@ -73,6 +74,8 @@ } + + /* * stop_server(svr_socket) * svr_socket: The socket that was created in the boot_server() @@ -121,7 +124,7 @@ int stop_server(int svr_socket) { * */ - int boot_server(char *ifaces, int port) { +int boot_server(char *ifaces, int port) { int svr_socket; int ret; struct sockaddr_in addr; @@ -172,41 +175,6 @@ int stop_server(int svr_socket) { return svr_socket; } -/* - * process_cli_requests(svr_socket) - */ -int process_cli_requests(int svr_socket) { - int cli_socket; - pthread_t thread_id; - - printf("SERVER: Waiting for client connections...\n"); - - while (1) { - printf("SERVER: Waiting for a client to connect...\n"); - - cli_socket = accept(svr_socket, NULL, NULL); - if (cli_socket == -1) { - perror("SERVER: accept failed"); - return ERR_RDSH_COMMUNICATION; - } - printf("SERVER: Client connected. Socket: %d\n", cli_socket); - - if (pthread_create(&thread_id, NULL, exec_client_requests, (void *)&cli_socket) < 0) { - perror("SERVER: Failed to create thread"); - close(cli_socket); - return ERR_RDSH_COMMUNICATION; - } - printf("SERVER: Thread created for client. Thread ID: %lu\n", (unsigned long)thread_id); - - pthread_detach(thread_id); - printf("SERVER: Thread detached.\n"); - } - - return OK; -} - - - /* * process_cli_requests(svr_socket) * svr_socket: The server socket that was obtained from boot_server() @@ -248,6 +216,8 @@ int process_cli_requests(int svr_socket) { * connections, and negative values terminate the server. * */ + + int process_cli_requests(int svr_socket) { int cli_socket; pthread_t thread_id; @@ -255,29 +225,30 @@ int process_cli_requests(int svr_socket) { printf("SERVER: Waiting for client connections...\n"); while (1) { - // Accept a client connection + printf("SERVER: Waiting for a client to connect...\n"); + cli_socket = accept(svr_socket, NULL, NULL); if (cli_socket == -1) { perror("SERVER: accept failed"); return ERR_RDSH_COMMUNICATION; } - printf("SERVER: Client connected. Socket: %d\n", cli_socket); - // Create a new thread to handle the client if (pthread_create(&thread_id, NULL, exec_client_requests, (void *)&cli_socket) < 0) { perror("SERVER: Failed to create thread"); close(cli_socket); return ERR_RDSH_COMMUNICATION; } + printf("SERVER: Thread created for client. Thread ID: %lu\n", (unsigned long)thread_id); - // Detach the thread to allow it to clean up automatically pthread_detach(thread_id); + printf("SERVER: Thread detached.\n"); } return OK; } + /* * exec_client_requests(cli_socket) * cli_socket: The server-side socket that is connected to the client @@ -320,7 +291,7 @@ int process_cli_requests(int svr_socket) { * or receive errors. */ - void *exec_client_requests(void *socket_handle) { +void *exec_client_requests(void *socket_handle) { int cli_socket = *((int *)socket_handle); char cmd_buff[RDSH_COMM_BUFF_SZ]; ssize_t recv_bytes;