Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

****NO ADDITIONAL CODE NEEDED. JUST NEED CONCISE EXPLANATIONS WHERE IT SAYS COMMENT HERE**** #include /* for printf() and fprintf() */ #include /* for socket(), bind(),

****NO ADDITIONAL CODE NEEDED. JUST NEED CONCISE EXPLANATIONS WHERE IT SAYS "COMMENT HERE"****

#include /* for printf() and fprintf() */ #include /* for socket(), bind(), and connect() */ #include /* for sockaddr_in and inet_ntoa() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */ #include

#define MAXPENDING 5 /* Maximum outstanding connection requests */ #define RCVBUFSIZE 32 /* Size of receive buffer */

void DieWithError(char *errorMessage); /* Error handling function */ void HandleTCPClient(int clntSocket); /* TCP client handling function */

int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned short echoServPort; /* Server port */ unsigned int clntLen; /* Length of client address data structure */ if (argc != 2) { fprintf(stderr, "Usage: %s ", argv[0]); exit(1); }

echoServPort = atoi(argv[1]); /* Comment here */

/* Comment here */ if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); memset(&echoServAddr, 0, sizeof(echoServAddr));/* Comment here */ echoServAddr.sin_family = AF_INET;/* Comment here */ echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);/* Comment here */ echoServAddr.sin_port = htons(echoServPort);/* Comment here */

/* Comment here */ if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("bind() failed");

/* Comment here */ if (listen(servSock, MAXPENDING) < 0) DieWithError("listen() failed");

for (;;) /* Run forever */ { clntLen = sizeof(echoClntAddr);

/* Comment here */ if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) DieWithError("accept() failed");

/* Comment here */

printf("Handling client %s ", inet_ntoa(echoClntAddr.sin_addr));

HandleTCPClient(clntSock); } /* NOT REACHED */ }

void HandleTCPClient(int clntSocket) { char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */ int recvMsgSize; /* Size of received message */ /* Comment here */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed"); /* Send received string and receive again until end of transmission */ while (recvMsgSize > 0) /* zero indicates end of transmission */ { /* Comment here */ send(clntSocket, echoBuffer, recvMsgSize, 0); /* Comment here */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed"); } close(clntSocket); /* Close client socket */ }

void DieWithError(char *errorMessage) { perror(errorMessage); exit(1); }

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

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions

Question

1. Who is responsible for resolving this dilemma?

Answered: 1 week ago