Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a UDP client program that sends and receives datagrams. It will have to se

ID: 3724315 • Letter: W

Question

Write a UDP client program that sends and receives datagrams. It will have to send and receive, with these actions being asynchronous. This can be done with threads or with non-blocking I/O.

Assume that there is a server c program that is going to "echo" the message back to your UDP client c program

In the main function Get time now, put in message, do sendto

In the thread use the recvfrom, to extract the time and Get time now, Extract time sent from message received, compute delta time, accumulate total delta times,increment num_received .

In the main function Wait for any straggler replies, Compute ave RTT, compute message loss percentage

If the main thread sleeps for 2 seconds, any straggler replies will have time to turn up and be seen by the recvfrom thread

Explanation / Answer

ANS)

Server Code
/************* UDP SERVER CODE *******************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;

/*Create UDP socket*/
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);

/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);  

/*Bind socket with address struct*/
bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

/*Initialize size variable to be used later on*/
addr_size = sizeof serverStorage;

while(1){
/* Try to receive any incoming UDP datagram. Address and port of
requesting client will be stored on serverStorage variable */
nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);

/*Convert message received to uppercase*/
for(i=0;i<nBytes-1;i++)
buffer[i] = toupper(buffer[i]);

/*Send uppercase message back to client, using serverStorage as the address*/
sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
}

return 0;
}
Client Code
/************* UDP CLIENT CODE *******************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;

/*Create UDP socket*/
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);

/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);  

/*Initialize size variable to be used later on*/
addr_size = sizeof serverAddr;

while(1){
printf("Type a sentence to send to server: ");
fgets(buffer,1024,stdin);
printf("You typed: %s",buffer);

nBytes = strlen(buffer) + 1;
  
/*Send message to server*/
sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);

/*Receive message from server*/
nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);

printf("Received from server: %s ",buffer);

}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote