Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Client Starter Code: /* Simple udp client */ #include #include #include #include #include #include #define SERVER 129.120.151.95 #define BUFLEN 512 //Max length of buffer #define

image text in transcribed

Client Starter Code:

/* Simple udp client */ #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), portno; 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; }

Server Starter Code:

/* Simple udp server */ #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()"); } //print details of the client/peer and the data received printf("Received packet from %s:%d ", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); printf("Received Data: %s " , buf); /ow 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; }

1. Create a C-based server that can run on cse02.cse.unt.edu and accepts single client's request using UDP socket 2. Create a C-based client that runs on cse03.cse.unt.edu and connects to the server 3. On the client side, create a UDP segment (as shown in Figure 1) using C structure that has the following fields: o 16-bit source port [Type: unsigned short int] o 16-bit destination port [Type: unsigned short int] o 16-bit length [Type: unsigned short int] o 16-bit checksum (Type: unsigned short int, computed after the header and payload is populated] o 256-byte payload (data) [Type: char] 4. Find the source port, destination port, and length and populate the corresponding fields of the UDP segment 5. Read the input (input.txt) text file (= 256 bytes) and populate the contents of the text file as the payload of the UDP segment 1. Create a C-based server that can run on cse02.cse.unt.edu and accepts single client's request using UDP socket 2. Create a C-based client that runs on cse03.cse.unt.edu and connects to the server 3. On the client side, create a UDP segment (as shown in Figure 1) using C structure that has the following fields: o 16-bit source port [Type: unsigned short int] o 16-bit destination port [Type: unsigned short int] o 16-bit length [Type: unsigned short int] o 16-bit checksum (Type: unsigned short int, computed after the header and payload is populated] o 256-byte payload (data) [Type: char] 4. Find the source port, destination port, and length and populate the corresponding fields of the UDP segment 5. Read the input (input.txt) text file (= 256 bytes) and populate the contents of the text file as the payload of the UDP segment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago

Question

=+j Describe an effective crisis management program.

Answered: 1 week ago