diff --git a/Assignment-06/starter/rsh_cli.c b/Assignment-06/starter/rsh_cli.c index 38798f0e76349762690dae11bdbb4efdffc9fe47..368056c37866612720b36c84d3daf18b92a87ded 100644 --- a/Assignment-06/starter/rsh_cli.c +++ b/Assignment-06/starter/rsh_cli.c @@ -91,6 +91,7 @@ * */ + int exec_remote_cmd_loop(char *address, int port) { char cmd_buff[RDSH_COMM_BUFF_SZ]; char rsp_buff[RDSH_COMM_BUFF_SZ]; @@ -98,16 +99,22 @@ int exec_remote_cmd_loop(char *address, int port) { ssize_t recv_bytes; int is_eof = 0; // Declare and initialize + // Debug: 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("start client"); + perror("CLIENT: Failed to start client"); return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_CLIENT); } + 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 } @@ -117,9 +124,14 @@ int exec_remote_cmd_loop(char *address, int port) { cmd_buff[cmd_len - 1] = '\0'; } + // Debug: Print the command received from the user + printf("CLIENT: Command received: %s\n", cmd_buff); + // Handle local commands (e.g., cd, exit) if (strncmp(cmd_buff, "cd", 2) == 0) { - // Handle cd locally + // Debug: Handling local 'cd' command + printf("CLIENT: Handling local 'cd' command...\n"); + cmd_buff_t cmd; if (build_cmd_buff(cmd_buff, &cmd) != OK) { fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT); @@ -137,30 +149,42 @@ 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 } + // 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("send"); + perror("CLIENT: Failed to send command"); return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_COMMUNICATION); } + 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("%.*s", (int)recv_bytes, rsp_buff); + 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 break; } } if (recv_bytes < 0) { - perror("recv"); + perror("CLIENT: Failed to receive response"); return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_COMMUNICATION); } else if (recv_bytes == 0) { - printf("Server closed the connection\n"); + printf("CLIENT: Server closed the connection.\n"); break; } } @@ -168,7 +192,6 @@ int exec_remote_cmd_loop(char *address, int port) { (void)is_eof; // Suppress unused variable warning return client_cleanup(cli_socket, NULL, NULL, OK); } - /* * start_client(server_ip, port) * server_ip: a string in ip address format, indicating the servers IP