Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*****There is no addition to the code needed. Just have to describe what each portion of the code does under the COMMENT HERE sections. I

*****There is no addition to the code needed. Just have to describe what each portion of the code does under the "COMMENT HERE" sections. I have done some but am having trouble with the rest******

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

#define ECHOMAX 255 /* Longest string to echo */

void DieWithError(char *errorMessage); /* External error handling function */

int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ struct sockaddr_in fromAddr; /* Source address of echo */ unsigned short echoServPort; /* Echo server port */ unsigned int fromSize; /* In-out of address size for recvfrom() */ char *servIP; /* IP address of server */ char *echoString; /* String to send to echo server */ char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */ int echoStringLen; /* Length of string to echo */ int respStringLen; /* Length of received response */

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

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

if ((echoStringLen = strlen(echoString)) > ECHOMAX) DieWithError("Echo word too long");

if (argc == 4) //checks to make sure there are 4 arguments echoServPort = atoi(argv[3]); //converts fourth parameter to an int and stores it in echoServPort else echoServPort = 7;

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

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

/* Comment here */ if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) != echoStringLen) DieWithError("sendto() sent a different number of bytes than expected"); fromSize = sizeof(fromAddr); /* Comment here */ if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen) DieWithError("recvfrom() failed");

/* Comment here */ if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) { fprintf(stderr,"Error: received a packet from unknown source. "); exit(1); }

/* Comment here */ echoBuffer[respStringLen] = '\0'; printf("Received: %s ", echoBuffer); 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

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions