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

C/C++ Add a function to the source code below so that if the client sends the wo

ID: 3801809 • Letter: C

Question

C/C++

Add a function to 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] = '';

       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;

}  

Explanation / Answer

#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<iomanip.h>
#include<dos.h>


class book_store
{
public:

char code[20],b_name[30],w_name[30];
float price;

void get_data()
{
cout<<" Book name :";
cin>>b_name;

cout<<" Book code : ";
cin>>code;

cout<<" Writer name :";
cin>>w_name;

cout<<" Price :";
cin>>price;
cout<<endl;
};

};


class book_file:public book_store
{
public:
void fil()
{
fstream file("All_Books.txt",ios::out|ios::app);

file.width(20);
file<<setiosflags(ios::left);
file<<b_name;

file.width(20);
file<<setiosflags(ios::left);
file<<w_name;

file.width(10);
file<<setiosflags(ios::right);
file<<code;

file.width(10);
file<<setiosflags(ios::right);
file<<price<<endl;

file.close();
}
void file();

};
void book_file::file()
{
char fil[20];

get_data();
strcpy(fil,code);
strcat(fil,".txt");

ofstream file(fil);
file<<"Book Name :"<<b_name<<endl;
file<<"Writer Name :"<<w_name<<endl;
file<<"Book Code :"<<code<<endl;
file<<"Price :"<<price<<endl;
file.close();

}


class T1:public book_file
{
public:

void book_data()
{
fstream file1("Literature.txt",ios::out|ios::app);
file1<<endl;
file1<<"Book Name :"<<b_name<<endl;
file1<<"Writer Name :"<<w_name<<endl;
file1<<"Book Code :"<<code<<endl;
file1<<"Price :"<<price<<endl;
file1.close();
}
};


class T2:public book_file
{
public:

void book_data()
{
fstream file1("Philosophy.txt",ios::out|ios::app);
file1<<endl;
file1<<"Book Name :"<<b_name<<endl;
file1<<"Writer Name :"<<w_name<<endl;
file1<<"Book Code :"<<code<<endl;
file1<<"Price :"<<price<<endl;
file1.close();
}
};

class T3:public book_file
{
public:

void book_data()
{
fstream file1("Science.txt",ios::out|ios::app);
file1<<endl;
file1<<"Book Name :"<<b_name<<endl;
file1<<"Writer Name :"<<w_name<<endl;
file1<<"Book Code :"<<code<<endl;
file1<<"Price :"<<price<<endl;
file1.close();
}
};

class T4:public book_file
{
public:

void book_data()
{
fstream file1("Comics.txt",ios::out|ios::app);
file1<<endl;
file1<<"Book Name :"<<b_name<<endl;
file1<<"Writer Name :"<<w_name<<endl;
file1<<"Book Code :"<<code<<endl;
file1<<"Price :"<<price<<endl;
file1.close();
}
}; //T4 class ends


class add_books
{
public:
add_books();
};

add_books::add_books()

{

T1 t1;
T2 t2;
T3 t3;
T4 t4;
int choice;

while(1)
{
clrscr();
cout<<"....................Add Books................";
cout<<" 1.Literature Books 2.Philosophy books 3.Science Books 4.Comics books 5.Exit ";
cout<<" Choice:";
cin>>choice;

if(choice==1)
{
t1.file();
t1.book_data();
t1.fil();

}

if(choice==2)
{
t2.file();
t2.book_data();
t2.fil();

}

if(choice==3)
{
t3.file();
t3.book_data();
t3.fil();

}

if(choice==4)
{
t4.file();
t4.book_data();
t4.fil();

}

if(choice==5)
{
break;
}

} //while ends

} //add_books fn ends


class display
{
public:
display();
};


display::display()
{
int choice;
char code[10],ch;
while(1)
{
cout<<" ...................DISPLAY..................... ";
cout<<"1.All Books"<<endl;
cout<<"2.Literature"<<endl;
cout<<"3.Philosophy"<<endl;
cout<<"4.Science"<<endl;
cout<<"5.Comics"<<endl;
cout<<"6.Exit"<<endl;

//...................................................................
cout<<" Enter Ur Choice:" ;
cin>>choice;

if(choice==1)
{
clrscr();

cout<<endl;
cout.width(20);
cout<<setiosflags(ios::left);
cout<<"Book Name";

cout.width(20);
cout<<setiosflags(ios::left);
cout<<"Writer Name";

cout.width(10);
cout<<setiosflags(ios::right);
cout<<"Code";

cout.width(10);
cout<<setiosflags(ios::right);
cout<<"Price"<<endl<<endl;

fstream file("All_Books.txt",ios::in);
file.seekg(0);
while(file)
{
file.get(ch);
cout<<ch;
}
file.close();
getch();
}

if(choice==2)
{
clrscr();
fstream file("Literature.txt",ios::in);
file.seekg(0);
cout<<" ";
while(file)
{
file.get(ch);
delay(90);
cout<<ch;
}
file.close();
getch();
}

if(choice==3)
{
clrscr();
fstream file("Philosophy.txt",ios::in);
file.seekg(0);
cout<<" ";
while(file)
{
file.get(ch);
delay(90);
cout<<ch;
}
file.close();
getch();
}

if(choice==4)
{
clrscr();
fstream file("Science.txt",ios::in);
file.seekg(0);
cout<<" ";
while(file)
{
file.get(ch);
delay(90);
cout<<ch;
}
file.close();
getch();
}

if(choice==5)
{
clrscr();
fstream file("Comics.txt",ios::in);
file.seekg(0);
cout<<" ";
while(file)
{
file.get(ch);
delay(90);
cout<<ch;
}
file.close();
getch();
}

if(choice==6)
break;

} // while (choice) ends
} // display class ends


class search
{
public:
search();
};

search::search()
{
int choice;
char fil[20];
char ch,code[10];
while(1)
{
clrscr();
cout<<" ....................SEARCH................... ";
cout<<"1.Search";
cout<<" 2.Exit ";
cout<<" Chioce:";
cin>>choice;
if(choice==1)
{
cout<<"Enter Code:";
cin>>code;
cout<<endl;
strcpy(fil,code);
strcat(fil,".txt");
fstream file(fil,ios::in);
while(file)
{
file.get(ch);
cout<<ch;
}
file.close();
getch();
}
if(choice==2)
break;
}


}

class start
{
public:
start();
};

start::start()
{
int choice;
textcolor(114);
textbackground(0);
highvideo();
while(1)
{
clrscr();
cout<<" .........Enter Ur Choice........ ";
cout<<"1.Add Books ";
cout<<"2.Display ";
cout<<"3.Search ";
cout<<"4.Exit ";
cout<<"Choice:";
cin>>choice;

if(choice==1)
{ add_books a;}

if(choice==2)
{ display d;}

if(choice==3)
{search s; }

if(choice==4)
break;

}

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