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: 3861429 • 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

TCP server Concurrent:multiple clients can be handled by this server.

This server handles 5 clients simultaneously.Hence concurrency is achieved by creating nw child process which process new client accepts while parent continues to accept new connections.

#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>
#include<stdlib.h>//sizeof
#include<netinet/in.h>

#define PORT 8000
#define MAXSZ 100
int main()
{
int sockfd;//to create socket
int newsockfd;//to accept connection

struct sockaddr_in serverAddress;//server receive on this address
struct sockaddr_in clientAddress;//server sends to client on this address

int n;
char msg[MAXSZ];
int clientAddressLength;
int pid;

//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);

//bind the socket with the server address and port
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));

//listen for connection from client
listen(sockfd,5);

while(1)
{
//parent process waiting to accept a new connection
printf(" *****server waiting for new client connection:***** ");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
printf("connected to client: %s ",inet_ntoa(clientAddress.sin_addr));

//child process is created for serving each new clients
pid=fork();
if(pid==0)//child process reciv and send
{
//rceive from client
while(1)
{
n=recv(newsockfd,msg,MAXSZ,0);
if(n==0)
{
close(newsockfd);
break;
}
msg[n]=0;
send(newsockfd,msg,n,0);

printf("Receive and set:%s ",msg);
}//close interior while
exit(0);
}
else
{
close(newsockfd);//sock is closed BY PARENT
}
}//close exterior while

return 0;
}

Client process:


#include<stdio.h>
#include<sys/types.h>//socket
#include<sys/socket.h>//socket
#include<string.h>//memset
#include<stdlib.h>//sizeof
#include<netinet/in.h>

#define PORT 8000
#define SERVER_IP "127.0.0.1"
#define MAXSZ 100
int main()
{
int sockfd;//to create socket

struct sockaddr_in serverAddress;//client will connect on this

int n;
char msg1[MAXSZ];
char msg2[MAXSZ];

//create socket
sockfd=socket(AF_INET,SOCK_STREAM,0);
//initialize the socket addresses
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr(SERVER_IP);
serverAddress.sin_port=htons(PORT);

//client connect to server on port
connect(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));
//send to sever and receive from server
while(1)
{
  printf(" Enter message to send to server: ");
  fgets(msg1,MAXSZ,stdin);
  if(msg1[0]=='#')
break;

  n=strlen(msg1)+1;
  send(sockfd,msg1,n,0);

  n=recv(sockfd,msg2,MAXSZ,0);

  printf("Receive message from server::%s ",msg2);
}

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