K. UDP File Transfer Program in Unix Using C Programming

UDF File Transfer Client

/*UDF File Transfer Client Example Program In C*/

#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>

main() {
   int sfd, cfd;
   char buf[1024] = "";
   struct sockaddr_in server, client;
   sfd = socket(AF_INET, SOCK_DGRAM, 0);
   bzero(&server, sizeof (server));
   server.sin_family = AF_INET;
   server.sin_port = htons(1011);
   inet_aton("172.16.29.67", &server.sin_addr);
   int b = connect(sfd, (struct sockaddr *) &server, sizeof (server));
   printf("CONNECT VALUE:%d\n", b);
   int l = sizeof (client);
   for (;;) {
      printf("ENTER THE FILE NAME\n");
      scanf("%s", buf);
      sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *) &server, sizeof (server));
      recvfrom(sfd, buf, 1024, 0, NULL, NULL);
      printf("RECEIVED FROM SERVER:%s\n", buf);
   }
   close(sfd);
}

UDF File Transfer Server

/*UDF File Transfer Server Example Program In C*/

#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>

main() {
   int sfd, cfd;
   char buf[1024] = "";
   struct sockaddr_in server, client;
   sfd = socket(AF_INET, SOCK_DGRAM, 0);
   bzero(&server, sizeof (server));
   server.sin_family = AF_INET;
   server.sin_port = htons(1011);
   inet_aton("172.16.29.67", &server.sin_addr);
   int b = bind(sfd, (struct sockaddr *) &server, sizeof (server));
   printf("BIND VALUE:%d\n", b);
   int l = sizeof (client);
   for (;;) {
      recvfrom(sfd, buf, 1024, 0, (struct sockaddr *) &client, &l);
      int fd = open(buf, O_RDONLY);
      read(fd, buf, 1024);
      sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *) &client, sizeof (client));
      printf("MESSAGE FROM CLIENT:%s\n", buf);
   }
   close(sfd);
}

Sample Output Server

$cc udps.c -o udps 
$./udps
BIND VALUE:0 
accept value 4 
MESSAGE FROM CLIENT: 
#include<stdio.h> 
main() 
{ 
printf("hello"); 
} 

Sample Output Client

#cc udps.c -o udps 
#./udps 
ENTER THE MESSAGE 
hello.c 
RECEIVED FROM SERVER
#include<stdio.h> 
main() 
{ 
printf("hello"); 
}