Skip to content
Snippets Groups Projects
Commit 9c50e99c authored by Joey Le's avatar Joey Le
Browse files

Added debugging to exec_remote_cmd_loop

parent cc9fae4f
Branches
No related tags found
No related merge requests found
...@@ -91,6 +91,7 @@ ...@@ -91,6 +91,7 @@
* *
*/ */
int exec_remote_cmd_loop(char *address, int port) { int exec_remote_cmd_loop(char *address, int port) {
char cmd_buff[RDSH_COMM_BUFF_SZ]; char cmd_buff[RDSH_COMM_BUFF_SZ];
char rsp_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) { ...@@ -98,16 +99,22 @@ int exec_remote_cmd_loop(char *address, int port) {
ssize_t recv_bytes; ssize_t recv_bytes;
int is_eof = 0; // Declare and initialize 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 // Connect to the server
cli_socket = start_client(address, port); cli_socket = start_client(address, port);
if (cli_socket < 0) { if (cli_socket < 0) {
perror("start client"); perror("CLIENT: Failed to start client");
return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_CLIENT); return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_CLIENT);
} }
printf("CLIENT: Successfully connected to server. Socket: %d\n", cli_socket);
while (1) { while (1) {
// Prompt the user for input
printf("rsh> "); printf("rsh> ");
if (!fgets(cmd_buff, RDSH_COMM_BUFF_SZ, stdin)) { 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; // Exit on input error or EOF
} }
...@@ -117,9 +124,14 @@ int exec_remote_cmd_loop(char *address, int port) { ...@@ -117,9 +124,14 @@ int exec_remote_cmd_loop(char *address, int port) {
cmd_buff[cmd_len - 1] = '\0'; 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) // Handle local commands (e.g., cd, exit)
if (strncmp(cmd_buff, "cd", 2) == 0) { 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; cmd_buff_t cmd;
if (build_cmd_buff(cmd_buff, &cmd) != OK) { if (build_cmd_buff(cmd_buff, &cmd) != OK) {
fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT); fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
...@@ -137,30 +149,42 @@ int exec_remote_cmd_loop(char *address, int port) { ...@@ -137,30 +149,42 @@ int exec_remote_cmd_loop(char *address, int port) {
free(cmd._cmd_buffer); free(cmd._cmd_buffer);
continue; continue;
} else if (strncmp(cmd_buff, "exit", 4) == 0) { } 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; // 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 // Send the command to the server
ssize_t sent_bytes = send(cli_socket, cmd_buff, strlen(cmd_buff), 0); ssize_t sent_bytes = send(cli_socket, cmd_buff, strlen(cmd_buff), 0);
if (sent_bytes < 0) { if (sent_bytes < 0) {
perror("send"); perror("CLIENT: Failed to send command");
return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_COMMUNICATION); 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 // Receive the server's response
while ((recv_bytes = recv(cli_socket, rsp_buff, RDSH_COMM_BUFF_SZ, 0)) > 0) { 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) { 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; // Mark EOF
break; break;
} }
} }
if (recv_bytes < 0) { if (recv_bytes < 0) {
perror("recv"); perror("CLIENT: Failed to receive response");
return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_COMMUNICATION); return client_cleanup(cli_socket, NULL, NULL, ERR_RDSH_COMMUNICATION);
} else if (recv_bytes == 0) { } else if (recv_bytes == 0) {
printf("Server closed the connection\n"); printf("CLIENT: Server closed the connection.\n");
break; break;
} }
} }
...@@ -168,7 +192,6 @@ int exec_remote_cmd_loop(char *address, int port) { ...@@ -168,7 +192,6 @@ int exec_remote_cmd_loop(char *address, int port) {
(void)is_eof; // Suppress unused variable warning (void)is_eof; // Suppress unused variable warning
return client_cleanup(cli_socket, NULL, NULL, OK); return client_cleanup(cli_socket, NULL, NULL, OK);
} }
/* /*
* start_client(server_ip, port) * start_client(server_ip, port)
* server_ip: a string in ip address format, indicating the servers IP * server_ip: a string in ip address format, indicating the servers IP
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment