Question: Write a program to make the UDP server program in Table 25.1 more generic: to receive a request, to process the request, and to send
Write a program to make the UDP server program in Table 25.1 more generic: to receive a request, to process the request, and to send back the response.
Table 25.1

Table 25.1 Echo server program using UDP 1 / UDP echo server program 2 #include "headerFiles.h" 3 int main (void) { II Declare and define variables int s; I/ Socket descriptor (reference) // Length of string to be echoed I/ Data buffer int len; 8 char buffer [256]; struct sockaddr_in servAddr; // Server (local) socket address // Client (remote) socket address 10 struct sockaddr_in clntAddr; 11 int clntAddrLen; I/ Length of client socket address I/ Build local (server) socket address memset (&servAddr, 0, sizeof (servAddr)); 12 II Allocate memory 13 // Family field I/ Default port number 14 servAddr.sin_family = AF_INET; 15 servAddr.sin_port = htons (SERVER_PORT) servAddr.sin_addr.s_addr=htonl(INADDR_ANY); I/ Default IP address I/ Create socket if (s = socket (PF_INET, SOCK_DGRAM, 0) < 0); 16 17 18 19 20 perror ("Error: socket failed!"); 21 exit (1); 22 23 I/ Bind socket to local address and port 24 if (bind (s, (struct sockaddr*) &servAddr, sizeof (servAddr) < 0); 25 26 perror ("Error: bind failed!"); 27 exit (1); 28 29 for (; ;) I/ Run forever 30 { I/ Receive String 31 32 len = recvfrom (s, buffer, sizeof (buffer), 0, 33 (struct sockaddr*)&clntAddr, &clntAddrLen); 34 I/ Send String 35 sendto (s, buffer, len, 0, (struct sockaddr*)&clntAddr, sizeof(clntAddr); }// End of for loop 37 } // End of echo server program 36 4799
Step by Step Solution
3.33 Rating (171 Votes )
There are 3 Steps involved in it
This is the general UDP server program in the C language in which the server receives an array of ... View full answer
Get step-by-step solutions from verified subject matter experts
