Question: Refer to the code snippet below and answer the following questions: (a) To what host IP address and what port the program attempts to connect?
Refer to the code snippet below and answer the following questions: (a) To what host IP address and what port the program attempts to connect? (b) What transport protocol is used? (c) Is there any checking performed in this snippet (including underlying transport and network protocols) to ensure that the other end has accepted the connection and is receiving data? Explain why. (d) How many packets are exchanged between the computer running the program and the remote host if the function call near the end of the snippet (send(sfd, data, len, 0)) is successful? int main() { struct addrinfo hints; struct addrinfo *result , *rp; int sfd, s, j; size, t len; ssize, t nread; char *data; struct sockaddr_storage peer_addr; socklen_t peer_addr_len; memset (&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; /* Allow IPv4 only (see man socket) */ hints.ai_socktype = SOCKDGRAM; hints.ai_flags = 0; hints.ai_protocol = IPPROTO_UDP; s = getaddrinfo(localhost, 2432, &hints, &result); if (s ! = 0) { fprintf(stderr, getaddrinfo : %s , gai_strerror (s)); exit (EXIT_FAILURE); } /* getaddrinfo() returns a list of address structures. Try each address until we successfully connect(2). If socket(2) (or connect (2) fails, we (close the socket and) try the next address. */ for (rp = result; rp ! = NULL; rp = rp rightarrow ai_next) { sfd = socket (rp rightarrow ai_family, rp rightarrow) ai_socktype, rp rightarrow ai_protocol); if (sfd = 1) continue; if (connect (sfd, rp rightarrow ai_addr, rp rightarrow ai_address) ! = 1) break; close (sfd); } if (rp = NULL) { /* No address succeeded */ fprintf (stderr, *Could_not_connect *); exit (EXIT_FAILURE); } freeaddrinfo ( result ); data = Port_number_is:_34893; len = strlen (data) + 1; if (send (sfd, data, len, 0) ! = len) { fprintf (stderr, partial/failed_write ); exit (EXIT_FAILURE); } /* Remaining code omitted */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
