Question: Assume we want to make the TCP client program in Table 25.2 more generic to be able to send a string and to handle the
Assume we want to make the TCP client program in Table 25.2 more generic to be able to send a string and to handle the response received from the server. Show how this can be done.

Table 25.2 Echo client program using UDP // UDP echo client program #include "headerFiles.h" int main (int arge, char* argv[ ]) // Three arguments to be checked later // Declare and define variables int s; I/ Socket descriptor 6. // Length of string to be echoed // Server name int len; char* servName; I/ Server port I/ String to be echoed // Data buffer int servPort; 10 char* string; char buffer [256 + 1]; 12 I/ Server socket address struct sockaddr_in servAddr; I/ Check and set program arguments if (argc !=3) 13 14 15 16 printf ("Error: three arguments are needed!"); exit(1): 19 servName=argv[1]; servPort = atoi (argv[2]); string = argv[3]; I/ Build server socket address memset (&servVAddr, 0, sizeof (servAddr)); servAddr.sin_family = AF_INET; inet_pton (AF_INET, servName, &servAddr.sin addr): servAddr.sin port = htons (servPort): // Create socket if ((s = socket (PF_INET, SOCK_DGRAM, 0) < 0); perror ("Error: Socket failed!"): exit (1); I/ Send echo string len = sendto (s, string, strlen (string), 0, (struct sockaddr)&servAddr, sizeof (servAddr); I/ Receive echo string recvfrom (s, buffer, len, 0, NULL, NULL); // Print and verify echoed string 38 buffer [len] = '\0'; printf ("Echo string received: "; fputs (buffer, stdout); I/ Close the socket 40 41 close (s);s 43 // Stop the program 44 exit (0): 45 J// End of echo client program
Step by Step Solution
3.49 Rating (159 Votes )
There are 3 Steps involved in it
Instead of using just one buffer we need to have two buffers recvBuff... View full answer
Get step-by-step solutions from verified subject matter experts
