diff --git a/Assignment-06/starter/rsh_cli.c b/Assignment-06/starter/rsh_cli.c index 04110740b877a015e1419a65ce78f26b2e8f2d17..57ebce8ed2d46b6c11484631dd176099544dfbfc 100644 --- a/Assignment-06/starter/rsh_cli.c +++ b/Assignment-06/starter/rsh_cli.c @@ -99,10 +99,9 @@ int exec_remote_cmd_loop(char *address, int port) { ssize_t recv_bytes; int is_eof = 0; - // Debug: Starting the client + // Debug for starting the client printf("CLIENT: Starting client. Connecting to server at %s:%d...\n", address, port); - // Connect to the server cli_socket = start_client(address, port); if (cli_socket < 0) { perror("CLIENT: Failed to start client"); @@ -111,25 +110,22 @@ int exec_remote_cmd_loop(char *address, int port) { printf("CLIENT: Successfully connected to server. Socket: %d\n", cli_socket); while (1) { - // Prompt the user for input printf("rsh> "); if (!fgets(cmd_buff, RDSH_COMM_BUFF_SZ, stdin)) { printf("CLIENT: Input error or EOF detected. Exiting...\n"); - break; // Exit on input error or EOF + break; } - // Remove the newline character from the command size_t cmd_len = strlen(cmd_buff); if (cmd_len > 0 && cmd_buff[cmd_len - 1] == '\n') { cmd_buff[cmd_len - 1] = '\0'; } - // Debug: Print the command received from the user + + //Debug printing received command printf("CLIENT: Command received: %s\n", cmd_buff); - // Handle local commands (e.g., cd, exit) if (strncmp(cmd_buff, "cd", 2) == 0) { - // Debug: Handling local 'cd' command printf("CLIENT: Handling local 'cd' command...\n"); cmd_buff_t cmd; @@ -149,15 +145,12 @@ int exec_remote_cmd_loop(char *address, int port) { free(cmd._cmd_buffer); continue; } else if (strncmp(cmd_buff, "exit", 4) == 0) { - // Debug: Handling 'exit' command printf("CLIENT: Handling 'exit' command. Exiting...\n"); - break; // Exit the remote shell + break; } - // Debug: Sending command to the server printf("CLIENT: Sending command to server: %s\n", cmd_buff); - // Send the command to the server ssize_t sent_bytes = send(cli_socket, cmd_buff, strlen(cmd_buff), 0); if (sent_bytes < 0) { perror("CLIENT: Failed to send command"); @@ -165,17 +158,14 @@ int exec_remote_cmd_loop(char *address, int port) { } printf("CLIENT: Command sent successfully. Bytes sent: %zd\n", sent_bytes); - // Debug: Receiving response from the server printf("CLIENT: Waiting for server response...\n"); - // Receive the server's response while ((recv_bytes = recv(cli_socket, rsp_buff, RDSH_COMM_BUFF_SZ, 0)) > 0) { printf("CLIENT: Received %zd bytes from server: %.*s\n", recv_bytes, (int)recv_bytes, rsp_buff); - // Check if the last byte is EOF if (rsp_buff[recv_bytes - 1] == RDSH_EOF_CHAR) { printf("CLIENT: EOF detected. End of server response.\n"); - is_eof = 1; // Mark EOF + is_eof = 1; break; } } @@ -222,7 +212,6 @@ int start_client(char *server_ip, int port){ int cli_socket; int ret; - // TODO set up cli_socket cli_socket = socket(AF_INET, SOCK_STREAM, 0); if (cli_socket < 0) { perror("socket"); diff --git a/Assignment-06/starter/rsh_server.c b/Assignment-06/starter/rsh_server.c index bf4764da99f5ad5f97c611e4a818c2c33d5bae1a..42e1b4de227e0de9b49efe165ab2498ffd5f7c8b 100644 --- a/Assignment-06/starter/rsh_server.c +++ b/Assignment-06/starter/rsh_server.c @@ -58,18 +58,15 @@ int start_server(char *ifaces, int port, int is_threaded) { svr_socket = boot_server(ifaces, port); if (svr_socket < 0) { int err_code = svr_socket; - printf("SERVER: Failed to boot server. Error code: %d\n", err_code); + printf("SERVER: Failed to boot server."); 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; } @@ -86,7 +83,6 @@ int start_server(char *ifaces, int port, int is_threaded) { */ int stop_server(int svr_socket) { - printf("SERVER: Closing server socket: %d\n", svr_socket); return close(svr_socket); } @@ -136,28 +132,23 @@ int boot_server(char *ifaces, int port) { 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("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("SERVER: inet_pton failed"); + //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("SERVER: bind failed"); close(svr_socket); return ERR_RDSH_SERVER; } @@ -498,9 +489,8 @@ int send_message_string(int cli_socket, char *buff) { int pids_st[clist->num]; // Array to store process statuses int exit_code; - printf("SERVER: Executing pipeline with %d commands.\n", clist->num); + //printf("SERVER: Executing pipeline with %d commands.\n", clist->num); - // Create all necessary pipes for (int i = 0; i < clist->num - 1; i++) { if (pipe(pipes[i]) == -1) { perror("SERVER: pipe failed"); @@ -520,7 +510,6 @@ int send_message_string(int cli_socket, char *buff) { if (pids[i] == 0) { // Child process printf("SERVER: Child process %d executing command: %s\n", i, clist->commands[i].argv[0]); - // Redirect input for the first command if (i == 0) { if (dup2(cli_sock, STDIN_FILENO) == -1) { perror("SERVER: dup2 stdin failed"); @@ -534,9 +523,7 @@ int send_message_string(int cli_socket, char *buff) { } } - // Redirect output for the last command if (i == clist->num - 1) { - // Redirect stdout and stderr to the client socket if (dup2(cli_sock, STDOUT_FILENO) == -1) { perror("SERVER: dup2 stdout failed"); exit(EXIT_FAILURE); @@ -546,14 +533,12 @@ int send_message_string(int cli_socket, char *buff) { exit(EXIT_FAILURE); } } else { - // Redirect stdout to the write end of the current pipe if (dup2(pipes[i][1], STDOUT_FILENO) == -1) { perror("SERVER: dup2 stdout failed"); exit(EXIT_FAILURE); } } - // Close all pipe ends in the child process for (int j = 0; j < clist->num - 1; j++) { close(pipes[j][0]); close(pipes[j][1]); @@ -566,20 +551,15 @@ int send_message_string(int cli_socket, char *buff) { } } - // Parent process: close all pipe ends for (int i = 0; i < clist->num - 1; i++) { close(pipes[i][0]); close(pipes[i][1]); } - // Wait for all children - printf("SERVER: Parent process waiting for children...\n"); for (int i = 0; i < clist->num; i++) { waitpid(pids[i], &pids_st[i], 0); - printf("SERVER: Child process %d exited with status: %d\n", i, WEXITSTATUS(pids_st[i])); } - // Determine the exit code exit_code = WEXITSTATUS(pids_st[clist->num - 1]); for (int i = 0; i < clist->num; i++) { if (WEXITSTATUS(pids_st[i]) == EXIT_SC) { diff --git a/Assignment-06/starter/rshlib.h b/Assignment-06/starter/rshlib.h index 0907a9ff8fd90e0e53bc2c7b2319a2a17ac9318b..e53cd9de170147a443e77aa4d597980e7eb96957 100644 --- a/Assignment-06/starter/rshlib.h +++ b/Assignment-06/starter/rshlib.h @@ -1,5 +1,5 @@ #ifndef __RSH_LIB_H__ - #define __RSH_LIB_H__ +#define __RSH_LIB_H__ #include "dshlib.h"