Question: Write a program to make the TCP client program in Table 25.4 more generic to be able to send any request created by the program.

Write a program to make the TCP client program in Table 25.4 more generic to be able to send any request created by the program.

Table 25.4 Echo client program using TCP 1 / TCP echo client

Table 25.4 Echo client program using TCP 1 / TCP echo client program 2 #include "headerFiles.h" 3 int main (int argc, char* argv[ ]) // Three arguments to be checked later 4 { I/ Declare and define // Socket descriptor // Number of bytes in each recv call 6. int s; 7 int n; char* servName; I/ Server name I/ Server port number I/ String to be echoed 9 int servPort; 10 char* string; 11 int len; // Length of string to be echoed I/ Buffer // Pointer to move along the buffer I/ Server socket address 12 char buffer [256 + 1]: 13 char* ptr = buffer; 14 struct sockaddr_in serverAddr; // Check and set arguments 15 16 if (arge !- 3) 17 18 printf ("Error: three arguments are needed!"); 19 exit (1); 20 21 servName = arg[1]: 22 servPort = atoi (arg [2]); 23 string = arg [3]; 24 // Create remote (server) socket address 25 memset (&servAddr, 0, sizeof(servAddr); serverAddr.sin family = AF INET; inet_pton (AF_INET, servName, &serverAddr.sin addr); / Server IP address 26 27 28 // Server port number serverAddr.sin_port = htons (servPort); // Create socket 29 30 if ((s = socket (PF_INET, SOCK_STREAM, 0) < 0); 31 32 perror ("Error: socket creation failed!"); 33 exit (1); 34 35 // Connect to the server 36 if (connect (sd, (struct sockaddr*)&servAddr, sizeof(servAddr)) < 0): 37 38 perror ("Error: connection failed!"); 39 exit (1); 40 41 // Data transfer section 42 send (s, string, strlen(string), 0); 43 while (n = recv (s, ptr, maxLen, 0)) > 0) 44 // Move pointer along the buffer Il Adjust the maximum number of bytes I/ Update the length of string received 45 ptr + = n; 46 maxLen - = n; 47 len += n; 48 J// End of while loop 49 I/ Print and verify the echoed string 50 buffer [len] = '\0'; 51 printf ("Echoed string received: "); fputs (buffer, stdout); I/ Close socket 52 53 54 close (s); 55 Il Stop program 56 exit (0); 57 }// End of echo client program

Step by Step Solution

3.26 Rating (164 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

We have used same strategy as in Prg22 here with some changes Creating the sending data is ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Networking Questions!