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++ Your problem

ID: 3861412 • Letter: W

Question

Write a threaded echo client / shell program using sockets in C/C++
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++
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++
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 socket server example

*/

#include<stdio.h>

#include<string.h>    //strlen

#include<sys/socket.h>

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

#include<unistd.h>    //write

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

{

    int socket_desc , client_sock , c , read_size;

    struct sockaddr_in server , client;

    char client_message[2000];

     

    //Create socket

    socket_desc = socket(AF_INET , SOCK_STREAM , 0);

    if (socket_desc == -1)

    {

        printf("Could not create socket");

    }

    puts("Socket created");

     

    //Prepare the sockaddr_in structure

    server.sin_family = AF_INET;

    server.sin_addr.s_addr = INADDR_ANY;

    server.sin_port = htons( 8888 );

     

    //Bind

    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)

    {

        //print the error message

        perror("bind failed. Error");

        return 1;

    }

    puts("bind done");

     

    //Listen

    listen(socket_desc , 3);

     

    //Accept and incoming connection

    puts("Waiting for incoming connections...");

    c = sizeof(struct sockaddr_in);

     

    //accept connection from an incoming client

    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);

    if (client_sock < 0)

    {

        perror("accept failed");

        return 1;

    }

    puts("Connection accepted");

     

    //Receive a message from client

    while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )

    {

        //Send the message back to client

        write(client_sock , client_message , strlen(client_message));

    }

     

    if(read_size == 0)

    {

        puts("Client disconnected");

        fflush(stdout);

    }

    else if(read_size == -1)

    {

        perror("recv failed");

    }

     

    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