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(), connect(),

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

#include /* for printf() and fprintf() */ #include /* for socket(), connect(), send(), and recv() */ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */

#define RCVBUFSIZE 32 /* Size of receive buffer */

void DieWithError(char *errorMessage); /* Error handling function */

int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ unsigned short echoServPort; /* Echo server port */ char *servIP; /* Server IP address (dotted quad) */ char *echoString; /* String to send to echo server */ char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */ unsigned int echoStringLen; /* Length of string to echo */ int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv() and total bytes read */

if ((argc < 3) || (argc > 4)) { fprintf(stderr, "Usage: %s [] ", argv[0]); exit(1); }

servIP = argv[1]; echoString = argv[2];

if (argc == 4) echoServPort = atoi(argv[3]); /* Comment here */ else echoServPort = 7;

/* Comment here */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed");

/* Comment here */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Comment here */ echoServAddr.sin_family = AF_INET; /* Comment here */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Comment here */ echoServAddr.sin_port = htons(echoServPort); /* Comment here */

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

echoStringLen = strlen(echoString);

/* Comment here */ if (send(sock, echoString, echoStringLen, 0) != echoStringLen) DieWithError("send() sent a different number of bytes than expected");

totalBytesRcvd = 0; printf("Received: "); while (totalBytesRcvd < echoStringLen) { /* Comment here */ if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0) DieWithError("recv() failed or connection closed prematurely"); totalBytesRcvd += bytesRcvd; echoBuffer[bytesRcvd] = '\0'; printf("%s", echoBuffer); }

printf(" "); /* Print a final linefeed */

close(sock); exit(0); }

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

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions