Question
Simulate sending of a text file using a UDP segment over a UDP socket. Requirements: Create a C based client-server architecture using UDP socket The
Simulate sending of a text file using a UDP segment over a UDP socket.
Requirements:
- Create a C based client-server architecture using UDP socket
- The server should run on cse02.cse.unt.edu machine and the client should run on cse03.cse.unt.edu machine
- Create a UDP segment at the client side and populate the fields (source port, destination port, length, and payload) of the segment. The payload (data) should have the contents of the input text file ( 256 bytes)
- The client should compute the checksum of the UDP segment and transmit the segment to the server
- The server should receive the UDP segment and verify the checksum. If the checksum is right, then write the payload to an output text file and if the checksum is wrong discard the segment
- Print on the console and write to log files (server.log and client.log) the UDP segment fields that are transmitted by the client and received by the server.
Procedure:
- Create a C-based server that can run on cse02.cse.unt.edu and accepts single clients request using UDP socket
- Create a C-based client that runs on cse03.cse.unt.edu and connects to the server
- On the client side, create a UDP segment (as shown in Figure 1) using C structure that has the following fields:
- 16-bit source port [Type: unsigned short int]
- 16-bit destination port [Type: unsigned short int]
- 16-bit length [Type: unsigned short int]
- 16-bit checksum [Type: unsigned short int, computed after the header and payload is populated]
- 256-byte payload (data) [Type: char]
- Find the source port, destination port, and length and populate the corresponding fields of the UDP segment
- Read the input (input.txt) text file ( 256 bytes) and populate the contents of the text file as the payload of the UDP segment
- Compute the 16-bit checksum for the entire UDP segment and populate the checksum field and transmit the segment
- Print on the client console and write to a client.log file the values of source port, destination port, length, checksum, and payload of the segment that is transmitted
- The server will receive the UDP segment from the client and compute the checksum of the received UDP segment.
- If the segment does not have any errors, then the payload is stored as an output text file (output.txt)
- Print on the server console and write to a server.log file the values of source port, destination port, length, checksum, and payload of the segment that is received
- If the segment has errors after checksum calculation, discard the segment. Print on the server console and write to a server.log file the error message saying the segment has errors
- Make sure the server port number and the input text file are sourced as arguments to the client as shown below
./uclient
- Run the server as
./userver
- Write to a file (server.log and client.log) and print to the console the UDP segment fields that are transmitted by the client and received by the server
Example UDP client code:
#include
#include
#include
#include
#include
#include
#define SERVER "129.120.151.95"
#define BUFLEN 512 //Max length of buffer
#define PORT 6700 //The port on which to send data
void die(char *s){
perror(s); exit(1);
}
int main(void){
struct sockaddr_in si_other;
int sockfd, i=0, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
die("socket");
}
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) {
fprintf(stderr, "inet_aton() failed ");
exit(1); }
while(1) {
//Clear the message buffer and accept the message to be sent
printf("Enter message: ");
bzero(message, sizeof(message));
do{message[i] = getchar();
i++;
}while (message[i-1] != ' ');
i = 0; //send the message
if (sendto(sockfd, message, strlen(message), 0, (struct sockaddr *) &si_other, slen) == -1) {
die("sendto()");
}
//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
bzero(buf, sizeof(buf));
//try to receive some data, this is a blocking call
if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1) {
die("recvfrom()");
}
printf("Received message from the server: ");
printf("%s ", buf);
}
close(sockfd);
return 0;
}
Example UDP server code:
#include
#include
#include
#include
#include
#include
#define BUFLEN 512 //Max length of buffer
#define PORT 6700 //The port on which to listen for incoming data
void die(char *s){
perror(s);
exit(1);
}
int main(void){
struct sockaddr_in si_me, si_other;
int sockfd, i, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
//create a UDP socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
die("socket");
}
// 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(sockfd, (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) {
die("bind"); }
//keep listening for data
while(1) {
printf("Waiting for data... ");
fflush(stdout); bzero (buf, BUFLEN);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1) {
die("recvfrom()"); }
printf("Received packet from %s:%d ", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Received Data: %s " , buf);
//now reply the client with the same data
if (sendto(sockfd, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) ==-1) {
die("sendto()");
}
bzero(buf, sizeof(buf));
}
close(sockfd);
return 0;}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started