Question: Attached are two UDP client and server programs written in C. These programs work on Linux (e.g., Ubuntu). To compile these, type gcc o client

Attached are two UDP client and server programs written in C. These programs work on Linux (e.g., Ubuntu). To compile these, type "gcc o client client.c" and "gcc o server server.c" in a terminal on Ubuntu. To run these, type ./client and ./server for each terminal.

Modify the UDP client and server posted above.

The UDP client sends 20 messages to the UDP server. (10 Points)

The UDP server replies to the client whenever 5 messages are received (or delivered), so the client receives only 4 messages from the server. (40 Points)

The UDP server does not reply if any of 5 messages is not delivered. (50 Points).

client :

// //** UDP client - CSCI 351, CSIT at UDC **// // #include #include #include #include #include #include #define SERVER "127.0.0.1" // local server #define BUFLEN 512 //Max length of buffer #define PORT 1234 //The port on which to send data void DieWithError(char *s) { perror(s); exit(1); } int main(void) { struct sockaddr_in si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; char message[BUFLEN]; int num; //number of messages if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { DieWithError("socket() failed"); } memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if (inet_aton(SERVER , &si_other.sin_addr) == 0) { DieWithError("inet_aton() failed"); } num = 0; //initial number is 0 while(1) { num++; sprintf(message, "%d CSCI 351", num); //send the message if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1) { DieWithError("sendto() failed"); } //clear the buffer by filling null, it might have previously received data memset(buf,'\0', BUFLEN); //try to receive some data, this is a blocking call if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1) { DieWithError("recvfrom() error"); } printf("%s ", buf); //print a received message if(num == 10) break; //send only 10 messages and break the while loop } close(s); // close your socket return 0; } 

server:

//** UDP server - CSCI 351, CSIT at UDC **// #include #include #include #include #include #include #define BUFLEN 512 //Max length of buffer #define PORT 1234 //The port on which to listen for incoming data void DieWithError(char *s) { perror(s); exit(1); } int main(void) { struct sockaddr_in si_me, si_other; int s, i, slen = sizeof(si_other) , recv_len; char buf[BUFLEN]; //create a UDP socket if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { DieWithError("socket() failed"); } // zero out the structure memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); //bind socket to port if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) { DieWithError("bind() failed"); } //keep listening for data while(1) { printf("Waiting for data..."); fflush(stdout); //try to receive some data, this is a blocking call if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1) { DieWithError("recvfrom() failed"); } //print details of the client/peer and the data received printf("Received a packet from %s:%d ", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); printf("Data: %s " , buf); //now reply the client with the same data if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) { DieWithError("sendto() failed"); } } close(s); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!