Question: Modify this server source code in C/C++ Modify the source code below so that if the client sends the word shell to the sever, all

Modify this server source code in C/C++

Modify the source code below so that if the client sends the word shell to the sever, all further commands from the client will be treated as shell commands. Return to echoing output when the user enters exit in the client.

Can not use system() commands, must use fork() and exec().

#include #include #include #include #include #include #include #include

//the thread function void *client_handler(void *);

int main(int argc, char *argv[])

{ /* Variables */ int sock, mysock, connection; struct sockaddr_in server, client;

char buff[1024]; int rval;

pthread_t thread_id;

/* Creat Socket */ sock = socket(AF_INET, SOCK_STREAM, 0); if(sock < 0) { perror("Failed to creat Socket"); exit(1); }

server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(5000) ;

/* Call Bind */

if(bind(sock, (struct sockaddr *)&server, sizeof(server))<0) { perror("Bind Failed"); }

/* Listen */

listen(sock,5);

puts("ready for listening and Waiting for incoming connections ");

/* Accept */ while(1) { socklen_t connection = sizeof(client);

//mysock = accept(sock, (struct sockaddr *)&client, (socklen_t *)&connection); mysock = accept(sock, (struct sockaddr *)&client, &connection);

puts("Connection accepted");

if(mysock == -1) { perror("accept failed"); } else { /* start a new thread but do not wait for it */ pthread_create(&thread_id, NULL, client_handler, (void *)&mysock); pthread_detach(thread_id); }

}

return 0;

}

void *client_handler(void *mysock) {

puts("Handler assigned"); //Get the socket descriptor int read_size; int sock_desk = *(int*)mysock; char *message , client_message[2000];

//Send some messages to the client message = "connection handler is ready "; write(sock_desk , message , strlen(message)); message = "server will repeat client message "; write(sock_desk , message , strlen(message)); //Receive a message from client while( (read_size = recv(sock_desk , client_message , 2000 , 0)) > 0 ) { //end of string marker client_message[read_size] = '\0';

puts(client_message); //Send the message back to client write(sock_desk , client_message , strlen(client_message)); //clear the message buffer memset(client_message, 0, 2000); } if(read_size == 0) { puts("Client disconnected"); fflush(stdout); } else if(read_size == -1) { perror("recv failed"); } return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!