C Programing - sending a file by using UDP segment over UDP socket.

profileTommy99
UDP_Server.c

/* Simple udp server */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #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...\n"); 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\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); printf("Received Data: %s\n" , 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; }