Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I uploaded the udp_client.zip file containing the provided program and code so it can be downloaded to make necessary changes. At the bottom I've also

I uploaded the udp_client.zip file containing the provided program and code so it can be downloaded to make necessary changes. At the bottom I've also pasted the main.c code, if you would rather create a new project from scratch.

It can only be tested on a specific network so obviously you won't be able to test, I am just having trouble with manipulating the code in order to store all of the packets properly.

https://www.dropbox.com/s/46mwaj31i0q9xza/udp_client%282%29.zip?dl=0

image text in transcribed

image text in transcribed

#include // Std i/o libraries - obviously #include // Std C library for utility fns & NULL defn #include // System calls, also declares STDOUT_FILENO #include // Pre-defined C error codes (in Linux) #include // String operations - surely, you know this! #include // Defns of system data types #include // Unix Socket libraries for TCP, UDP, etc. #include // INET constants #include // Conversion of IP addresses, etc. #include // Network database operations, incl. getaddrinfo

// Our constants .. #define MAXBUF 10000 // 4K max buffer size for i/o over nwk #define SRVR_PORT "5555" // the server's port# to which we send data // NOTE: ports 0 -1023 are reserved for superuser!

int main(int argc, char *argv[]) { int sockfd; // Socket file descriptor; much like a file descriptor struct addrinfo hints, *servinfo, *p; // Address structure and ptrs to them int rv, nbytes, nread; char buf[MAXBUF]; // Size of our network app i/o buffer

if (argc != 3) { fprintf(stderr,"ERROR! Correct Usage is: ./program_name server userid " "Where, server = server_name or ip_address, and " " userid = your LDAP (VU) userid "); exit(1); }

// First, we need to fill out some fields of the 'hints' struct memset(&hints, 0, sizeof hints); // fill zeroes in the hints struc hints.ai_family = AF_UNSPEC; // AF_UNSPEC means IPv4 or IPv6; don't care hints.ai_socktype = SOCK_DGRAM; // SOCK_DGRAM means UDP

// Then, we call getaddrinfo() to fill out other fields of the struct 'hints // automagically for us; servinfo will now point to the addrinfo structure // of course, if getaddrinfo() fails to execute correctly, it will report an // error in the return value (rv). rv=0 implies no error. If we do get an // error, then the function gai_strerror() will print it out for us if ((rv = getaddrinfo(argv[1], SRVR_PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s ", gai_strerror(rv)); return 1; }

// We start by pointing p to wherever servinfo is pointing; this could be // the very start of a linked list of addrinfo structs. So, try every one of // them, and open a socket with the very first one that allows us to // Note that if a socket() call fails (i.e. if it returns -1), we continue // to try opening a socket by advancing to the next node of the list // by means of the stmt: p = p->ai_next (ai_next is the next ptr, defined in // struct addrinfo). for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("CLIENT: socket"); continue; }

break; }

// OK, keep calm - if p==NULL, then it means that we cycled through whole // linked list, but did not manage to open a socket! We have failed, and // with a deep hearted sigh, accept defeat; with our tail between our legs, // we terminate our program here with error code 2 (from main). if (p == NULL) { fprintf(stderr, "CLIENT: failed to create socket "); return 2; }

// If p!=NULL, then things are looking up; the OS has opened a socket for // us and given us a socket descriptor. We are cleared to send! Hurray! // The sendto() function will report how many bytes (nbytes) it sent; but a // negative value (-1) means failure. Sighhh. if ((nbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, p->ai_addr, p->ai_addrlen)) == -1) { perror("CLIENT: sendto"); exit(1); }

printf("CLIENT: sent '%s' (%d bytes) to %s ", argv[2], nbytes, argv[1]);

// Recv packet from server. YOu should modify this part of the program so that // you can receive more than 1 packet from the server. In fact, you should // call recvfrom() repeatedly till all parts of the file have been received. nread = recvfrom(sockfd,buf,MAXBUF,0,NULL,NULL); if (nread printf("Received %d bytes ",nread); // Output recvd data to stdout; if (write(STDOUT_FILENO,buf,nread)

// AFTER all packets have been received .... // free up the linked-list memory that was allocated for us so graciously // getaddrinfo() above; and close the socket as well - otherwise, bad things // could happen freeaddrinfo(servinfo); close(sockfd);

printf(" "); // So that the new terminal prompt starts two lines below return 0; }

You are to develop the client-side software for a UDP (Datagram) socket. The communicati on code is given to you, but you must provide the following additions after understanding the following message exchange diagram: 1. Client Your Het erver rtavillincni.nu Understand how the supplied code works, at a high level. Next, locate the part of the code that prints out the contents of the incoming packet to your screen. In its place, do the following: Leeio hesia necks, DAT a. First of all, ensure that your program does indeed communicate with the server. You can run the program from the command line in your terminal by typing the following (first be sure to change to the src directory of the project where the executable resides): program name server userid b. The format of the first packet from the server to the client (in the above figure) is as follows: 4 bytes--4 bytes Variable bytes (max 92 bytes) e Size Chksum File Name File size represents the number of bytes (characters) in the file to be transmitted. It is stored as a sequence of four ASCII characters, for example, a file size of 120 is stored as ' ' , ' 1' , ' 2' ,' Chksum represents the checksum computed over the entire file to be transmitted. It is stored as a sequence of four ASCII characters, for example'7','A,'1','E File Name is also in ASCII and specifies the name that you must give to your output file. Create a directory called data files in your project directory, and in that directory, create a file whose file name is specified in the File Name field. c. Every subsequent incoming packet has the following field format: .. 2 bytes 1 byte-3 bytes Max 100 Bytes. Min l Byte-.............. Packet # LPFlag Payload Sz DATA PAYLOAD The packet is entire ly coded using ASCII characters. The fields are as follows: '9''9.After Packet# (i.e. sequence#) starts with '6''0' and can go up to rolls over to a ' ' '0' and counts up again. '9''9', the packet# LPFlag is '' if this pac ket is the last packet in the sequence; it is otherwse. Payload Sz is simply the size of the DATA PAYLOAD field, in bytes. All packets except the last one carry 100 bytes of data. The last packet may carry a smaller payload. For example, if a file of 248 bytes is sent to your client, it will be di vided up into three packets with payload sizes of 100, 100 and 48, respectively. Like the previous three fields, the payload is also encoded as an ASCII character sequence. Therefore, a payload of 100 is encoded as ' 1' ' In this lab exercise, you may assume that all packets will arive in order. Based on the specifications of the simple file transfer protocol explained above, write your code to store the incoming data in the file that you created in step 1.b. After your file is complete, you will find that it is a poem. You are to develop the client-side software for a UDP (Datagram) socket. The communicati on code is given to you, but you must provide the following additions after understanding the following message exchange diagram: 1. Client Your Het erver rtavillincni.nu Understand how the supplied code works, at a high level. Next, locate the part of the code that prints out the contents of the incoming packet to your screen. In its place, do the following: Leeio hesia necks, DAT a. First of all, ensure that your program does indeed communicate with the server. You can run the program from the command line in your terminal by typing the following (first be sure to change to the src directory of the project where the executable resides): program name server userid b. The format of the first packet from the server to the client (in the above figure) is as follows: 4 bytes--4 bytes Variable bytes (max 92 bytes) e Size Chksum File Name File size represents the number of bytes (characters) in the file to be transmitted. It is stored as a sequence of four ASCII characters, for example, a file size of 120 is stored as ' ' , ' 1' , ' 2' ,' Chksum represents the checksum computed over the entire file to be transmitted. It is stored as a sequence of four ASCII characters, for example'7','A,'1','E File Name is also in ASCII and specifies the name that you must give to your output file. Create a directory called data files in your project directory, and in that directory, create a file whose file name is specified in the File Name field. c. Every subsequent incoming packet has the following field format: .. 2 bytes 1 byte-3 bytes Max 100 Bytes. Min l Byte-.............. Packet # LPFlag Payload Sz DATA PAYLOAD The packet is entire ly coded using ASCII characters. The fields are as follows: '9''9.After Packet# (i.e. sequence#) starts with '6''0' and can go up to rolls over to a ' ' '0' and counts up again. '9''9', the packet# LPFlag is '' if this pac ket is the last packet in the sequence; it is otherwse. Payload Sz is simply the size of the DATA PAYLOAD field, in bytes. All packets except the last one carry 100 bytes of data. The last packet may carry a smaller payload. For example, if a file of 248 bytes is sent to your client, it will be di vided up into three packets with payload sizes of 100, 100 and 48, respectively. Like the previous three fields, the payload is also encoded as an ASCII character sequence. Therefore, a payload of 100 is encoded as ' 1' ' In this lab exercise, you may assume that all packets will arive in order. Based on the specifications of the simple file transfer protocol explained above, write your code to store the incoming data in the file that you created in step 1.b. After your file is complete, you will find that it is a poem

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions