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

Write two C programs, a client and a server, to implement a simple remote shell

ID: 3699884 • Letter: W

Question

Write two C programs, a client and a server, to implement a simple remote shell connection The server process and the client process will run on two different machines and the communi cation between the two processes is achieved using Sockets The server task can be summarized as follows: The server must start running before any client, and wait for connections When the server gets a client, forks and, let the child process take care of the client in a separate function, called serviceClient, while the parent process goes back to wait fo the next client. Then, the server's child process 1. uses "dup20 to make the screen descriptor designate the client socket 2. gets in an infinite loop then: - reads a shell command from the client's socket, if the client sends "quit", then the server's child, closes socket and quits. otherwise, it excutes command, using the "systemO" library function, The client process connects to the server, then . gets into an infinite loops 1. reads a command from keyboard, 2. write the command to the server, 3. if command is "quit", closes socket and quits 4. otherwise, reads command output from socket and displays them on the screen

Explanation / Answer

client.c

#include <stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<arpa/inet.h>

int main()
{
char fname[50],buffer[1024];
int s,n;
struct Sockaddr_in address;
address.sin_port = htons(15000);
address.sin_family=AF_INET;
address.sin_addr.s_addr=INADDR_ANY;
if((s=Socket(PF_INET,SOCK_STREAM,0))<0)
{
connect(s,(struct Sockaddr*)&address,sizeof(address));
}
printf("Enter file name ");
scanf("%s",fname);
printf(" sending request");
send(s,fname,sizeof(fname),0);
printf("Response Received");
while((n=recv(s,buffer,sizeof(buffer),0))>0)
{
write(1,buffer,n);
}
return 0;
}

server.c

#include <stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<arpa/inet.h>

int main()
{
char fname[50],buffer[1024];
int s,n,source,file;
struct Sockaddr_in address;
address.sin_port = htons(15000);
address.sin_family=AF_INET;
address.sin_addr.s_addr=INADDR_ANY;
printf("Waiting for request.. ");
s=Socket(PF_INET,SOCK_STREAM,0);
bind(s,(struct Sockaddr*)&address,sizeof(address));
listen(s,3);
source=accept(s,(struct Sockaddr*)NULL,NULL);
recv(source,fname,sizeof(fname),0);
printf("%s ",fname);
file=open(fname,O_RDONLY);
if(file<0)
{
send(source,"file not found ",20,0);
}
else
{
while((n=read(file,buffer,sizeof(buffer),0))>0)
{
send(source,buffer,n,0);
}
printf("request sent");
}
close(file);
return 0;
}