Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you guys check this is working I am having problem run the server tcp unix I need brief of screen shot #include #include #include

Can you guys check this is working

I am having problem run the server tcp unix I need brief of screen shot

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXPENDING 5
#define BUFFSIZE 2048
#define MAX 2048
void Die(char *mess)
{
perror(mess);
exit(1);
}
void setup(char inputBuffer[], char *args[], int *background) //Ham xu li (cat) chuoi lenh
{
const char s[4] = " \t ";
char *token;
token = strtok(inputBuffer, s);
int i = 0;
while( token != NULL)
{
args[i] = token;
i++;
//printf("%s ", token);
token = strtok(NULL,s);
}
args[i] = NULL;
}
void HandeClient(int sock){
char buffer[BUFFSIZE];
int received = -1;
char data[MAX];
data[0] = '\0';
if((received = recv(sock, buffer,BUFFSIZE,0))<0){
Die("Failed");
}
buffer[received] = '\0';
strcat (data, buffer);
puts (data);
char *args[100];
setup(data,args,0);
int pipefd[2],lenght;
if(pipe(pipefd))
Die("Failed to create pipe");
pid_t pid = fork();
char path[MAX];
if(pid==0)
{
close(1); // close the original stdout
dup2(pipefd[1],1); // duplicate pipefd[1] as stdout
close(pipefd[0]); // close the readonly end of the pipe
close(pipefd[1]); // close the original writeonly end of the pipe
execvp(args[0],args); // execute the command. this will not return and will exit at completion unless the executed args[0] doesn't end.
}
else
if(pid>0)
{
close(pipefd[1]);
memset(path,0,MAX);
while(lenght=read(pipefd[0],path,MAX-1)){
printf("%s ", path);
if(send(sock,path,strlen(path),0) != strlen(path) ){
Die("Failed");
}
fflush(NULL);
//printf("Data sent so far %s ", path);
memset(path,0,MAX);//
}
close(pipefd[0]);
//return
}
else
{
printf("Error ! ");
exit(0);//
}
if (strcmp (data, "exits")==0)
{
printf("Closing socket ");
exit (1);
}
close(sock);
}
int main(int argc, char const *argv[])
{
int serversock,clientsock;
struct sockaddr_in echoserver, echoclient;
signal(SIGPIPE, SIG_IGN); //ignoring SIGPIPE to allow the code to handle the failures instead of crashing with SIGPIPE.
if((serversock = socket(PF_INET, SOCK_STREAM,IPPROTO_TCP))<0){
Die("Failed");
}
memset(&echoserver,0,sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr= htonl(INADDR_ANY);
echoserver.sin_port = htons(atoi(argv[1]));
if(bind(serversock, (struct sockaddr *) & echoserver,sizeof(echoserver))<0){
Die("Failed");
}
if(listen(serversock,MAXPENDING)<0){
Die("Failed");
}
while(1)
{
unsigned int clientlen = sizeof(echoclient);
if((clientsock =
accept(serversock,(struct sockaddr *) &echoclient,
&clientlen))<0){
Die("Failed");
}
fprintf(stdout, "Client connected: %s ",
inet_ntoa(echoclient.sin_addr));
fprintf(stdout,"Message from client:");
HandeClient(clientsock);
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the various collection policies in receivables management.

Answered: 1 week ago

Question

What are the main objectives of Inventory ?

Answered: 1 week ago

Question

Explain the various inventory management techniques in detail.

Answered: 1 week ago

Question

Identify the different methods employed in the selection process.

Answered: 1 week ago

Question

Demonstrate the difference between ability and personality tests.

Answered: 1 week ago