From 9ebef86aa95d4690b362b66fafd42e5f55f7f572 Mon Sep 17 00:00:00 2001 From: jl4589 <jl4589@drexel.edu> Date: Thu, 13 Mar 2025 17:32:27 -0400 Subject: [PATCH] WHOLE LOT OF DEBUG --- Assignment-06/starter/rsh_server.c | 103 ++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/Assignment-06/starter/rsh_server.c b/Assignment-06/starter/rsh_server.c index 013d9ca..f10127f 100644 --- a/Assignment-06/starter/rsh_server.c +++ b/Assignment-06/starter/rsh_server.c @@ -46,26 +46,33 @@ * IF YOU IMPLEMENT THE MULTI-THREADED SERVER FOR EXTRA CREDIT YOU NEED * 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; + printf("SERVER: Starting server on %s:%d...\n", ifaces, port); + svr_socket = boot_server(ifaces, port); - if (svr_socket < 0){ - int err_code = svr_socket; //server socket will carry error code + if (svr_socket < 0) { + int err_code = svr_socket; + printf("SERVER: Failed to boot server. Error code: %d\n", err_code); return err_code; } + printf("SERVER: Server booted successfully. Socket: %d\n", svr_socket); + rc = process_cli_requests(svr_socket); + printf("SERVER: Stopping server...\n"); stop_server(svr_socket); - + printf("SERVER: Server stopped. Return code: %d\n", rc); return rc; } + /* * stop_server(svr_socket) * svr_socket: The socket that was created in the boot_server() @@ -74,9 +81,10 @@ int start_server(char *ifaces, int port, int is_threaded){ * This function simply returns the value of close() when closing * the socket. */ -int stop_server(int svr_socket){ - return close(svr_socket); +int stop_server(int svr_socket) { + printf("SERVER: Closing server socket: %d\n", svr_socket); + return close(svr_socket); } /* @@ -112,59 +120,93 @@ int stop_server(int svr_socket){ * bind(), or listen() call fails. * */ -int boot_server(char *ifaces, int port){ + + int boot_server(char *ifaces, int port) { int svr_socket; int ret; struct sockaddr_in addr; - // TODO set up the socket - this is very similar to the demo code + printf("SERVER: Booting server on %s:%d...\n", ifaces, port); svr_socket = socket(AF_INET, SOCK_STREAM, 0); if (svr_socket == -1) { - perror("socket"); + perror("SERVER: socket failed"); return ERR_RDSH_SERVER; } + printf("SERVER: Socket created. Socket: %d\n", svr_socket); int enable = 1; if (setsockopt(svr_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) { - perror("setsockopt"); + perror("SERVER: setsockopt failed"); close(svr_socket); return ERR_RDSH_SERVER; } + printf("SERVER: Socket options set.\n"); addr.sin_family = AF_INET; if (inet_pton(AF_INET, ifaces, &addr.sin_addr) <= 0) { - perror("inet_pton"); + perror("SERVER: inet_pton failed"); close(svr_socket); return ERR_RDSH_SERVER; } addr.sin_port = htons(port); + printf("SERVER: Address structure configured.\n"); - - ret = bind(svr_socket, (const struct sockaddr *) &addr, sizeof(struct sockaddr_in)); - - if (ret == -1 ) { - perror("bind"); + ret = bind(svr_socket, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)); + if (ret == -1) { + perror("SERVER: bind failed"); close(svr_socket); return ERR_RDSH_SERVER; } + printf("SERVER: Socket bound to address.\n"); - /* - * Prepare for accepting connections. The backlog size is set - * to 20. So while one request is being processed other requests - * can be waiting. - */ ret = listen(svr_socket, 20); if (ret == -1) { - perror("listen"); + perror("SERVER: listen failed"); close(svr_socket); return ERR_RDSH_COMMUNICATION; } - - printf("SERVER BOOTED on %s:%d\n", ifaces, port); + printf("SERVER: Listening for connections...\n"); + + printf("SERVER: Server booted successfully on %s:%d\n", ifaces, port); 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() @@ -397,8 +439,9 @@ int process_cli_requests(int svr_socket) { */ int send_message_eof(int cli_socket) { + printf("SERVER: Sending EOF to client. Socket: %d\n", cli_socket); if (send(cli_socket, &RDSH_EOF_CHAR, 1, 0) < 0) { - perror("send"); + perror("SERVER: send failed"); return ERR_RDSH_COMMUNICATION; } return OK; @@ -424,19 +467,17 @@ int send_message_eof(int cli_socket) { * ERR_RDSH_COMMUNICATION: The send() socket call returned an error or if * we were unable to send the message followed by the EOF character. */ -int send_message_string(int cli_socket, char *buff){ - int bytes_sent; - bytes_sent = send(cli_socket, buff, strlen(buff), 0); +int send_message_string(int cli_socket, char *buff) { + printf("SERVER: Sending message to client. Socket: %d\n", cli_socket); + int bytes_sent = send(cli_socket, buff, strlen(buff), 0); if (bytes_sent < 0) { - perror("send"); + perror("SERVER: send failed"); return ERR_RDSH_COMMUNICATION; } - return OK; } - /* * rsh_execute_pipeline(int cli_sock, command_list_t *clist) * cli_sock: The server-side socket that is connected to the client -- GitLab