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

UDP Receiver (C language) Write a receiver.c program for UDP. the receiver must

ID: 642693 • Letter: U

Question

UDP Receiver (C language)

Write a receiver.c program for UDP.

the receiver must:

1. check whether the file already exists, and if so, print an error message and exit.

2.. create the file.

3. repeatedly read packets, distinguishing new packets from retransmitted packets (and discarding any invalid packets as above)

4. save to the file the data of each new packet

5. send an ack for each received packet. Acks should be sent both for new packets and for retransmitted packets. The acks should be sent to the address that the packet was received from.

6. once a packet is received with less than 100 bytes of data, save this last packet (if it has more than 0 data bytes), ack it, close the file, and exit.

Explanation / Answer

1 #define SRV_IP "999.999.999.999" 2 /* diep(), #includes and #defines like in the server */ 3 4 int main(void) 5 { 6 struct sockaddr_in si_other; 7 int s, i, slen=sizeof(si_other); 8 char buf[BUFLEN]; 9 10 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) 11 diep("socket"); 12 13 memset((char *) &si_other, 0, sizeof(si_other)); 14 si_other.sin_family = AF_INET; 15 si_other.sin_port = htons(PORT); 16 if (inet_aton(SRV_IP, &si_other.sin_addr)==0) { 17 fprintf(stderr, "inet_aton() failed "); 18 exit(1); 19 } 20 21 for (i=0; i