Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a threaded echo client / shell program using sockets in C/C++ **READ ENTIR

ID: 3861459 • Letter: W

Question

Write a threaded echo client / shell program using sockets in C/C++
**READ ENTIRE QUESTION BEFORE ANSWERING**
Your problem is to add a "nice" feature to this program to allow a user to start a command-line shell on the host machine. Modify the source code from the lecture so that when the client sends the command "shell" to server, all further commands from the client will be treated as shell commands on the server. Return back to normal operation when the user enters the "exit" command in the client (i.e., return to just echoing output).
You code must support multiple connections to the server. This means that the server has to keep track of multiple sockets: one to listen for new connections and then a socket pair per connection to which you direct output from the server (either echo or standard output from running shell commands).
On the server, the naive approach would be to use the system() system call to execute the command sent from the client. This works but leaves too much information available for a security analyst to detect what program is doing the malicious deeds. Instead, use the code from the previous assignment and have the server code do a fork() and exec() to execute the command on the server. (EXTRA CREDIT: do what you can to mask the parent process id of the child process.) Write a threaded echo client / shell program using sockets in C/C++
**READ ENTIRE QUESTION BEFORE ANSWERING**
Your problem is to add a "nice" feature to this program to allow a user to start a command-line shell on the host machine. Modify the source code from the lecture so that when the client sends the command "shell" to server, all further commands from the client will be treated as shell commands on the server. Return back to normal operation when the user enters the "exit" command in the client (i.e., return to just echoing output).
You code must support multiple connections to the server. This means that the server has to keep track of multiple sockets: one to listen for new connections and then a socket pair per connection to which you direct output from the server (either echo or standard output from running shell commands).
On the server, the naive approach would be to use the system() system call to execute the command sent from the client. This works but leaves too much information available for a security analyst to detect what program is doing the malicious deeds. Instead, use the code from the previous assignment and have the server code do a fork() and exec() to execute the command on the server. (EXTRA CREDIT: do what you can to mask the parent process id of the child process.) Write a threaded echo client / shell program using sockets in C/C++
**READ ENTIRE QUESTION BEFORE ANSWERING**
Your problem is to add a "nice" feature to this program to allow a user to start a command-line shell on the host machine. Modify the source code from the lecture so that when the client sends the command "shell" to server, all further commands from the client will be treated as shell commands on the server. Return back to normal operation when the user enters the "exit" command in the client (i.e., return to just echoing output).
You code must support multiple connections to the server. This means that the server has to keep track of multiple sockets: one to listen for new connections and then a socket pair per connection to which you direct output from the server (either echo or standard output from running shell commands).
On the server, the naive approach would be to use the system() system call to execute the command sent from the client. This works but leaves too much information available for a security analyst to detect what program is doing the malicious deeds. Instead, use the code from the previous assignment and have the server code do a fork() and exec() to execute the command on the server. (EXTRA CREDIT: do what you can to mask the parent process id of the child process.)

Explanation / Answer

/*

    C ECHO client example using sockets

*/

#include<stdio.h> //printf

#include<string.h>    //strlen

#include<sys/socket.h>    //socket

#include<arpa/inet.h> //inet_addr

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

{

    int sock;

    struct sockaddr_in server;

    char message[1000] , server_reply[2000];

     

    //Create socket

    sock = socket(AF_INET , SOCK_STREAM , 0);

    if (sock == -1)

    {

        printf("Could not create socket");

    }

    puts("Socket created");

     

    server.sin_addr.s_addr = inet_addr("127.0.0.1");

    server.sin_family = AF_INET;

    server.sin_port = htons( 8888 );

    //Connect to remote server

    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)

    {

        perror("connect failed. Error");

        return 1;

    }

     

    puts("Connected ");

     

    //keep communicating with server

    while(1)

    {

        printf("Enter message : ");

        scanf("%s" , message);

         

        //Send some data

        if( send(sock , message , strlen(message) , 0) < 0)

        {

            puts("Send failed");

            return 1;

        }

         

        //Receive a reply from the server

        if( recv(sock , server_reply , 2000 , 0) < 0)

        {

            puts("recv failed");

            break;

        }

         

        puts("Server reply :");

        puts(server_reply);

    }

     

    close(sock);

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote