FTP APPLICATION USING UDP

Server program

#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<netinet/in.h>
main()
{
 int sd,l;
 int fd;
 char buff[100];
 struct sockaddr_in serv,cli;
 serv.sin_family=AF_INET;
 serv.sin_addr.s_addr=inet_addr("192.168.1.134");
 serv.sin_port=4827;
 printf("\n\t UDP server for file transferring ");
 sd=socket(AF_INET,SOCK_DGRAM,0);
 if(sd<0)
  printf("\n\t Error in creation of socket");
 else
  printf("\n\t server socket created");
  if (bind(sd,(struct sockaddr *)&serv,sizeof(serv)) < 0)
  {
    printf("\n\t Bind error");      exit(0);
  }
 printf("\n\t Server is ready .......");
 printf("waiting for client .......");
 l=sizeof(cli);
 recvfrom(sd,buff,sizeof(buff)+1,0,(struct sockaddr_in *)&cli,&l);
 printf("\n\t File Name from Client :%s",buff);
 printf("\n");
 fd=open(buff,O_RDONLY);
 if(fd==-1)
 {
  sendto(sd,"NO",20,0,(struct sockaddr_in *)&cli,sizeof(cli));
 }
 else
 {
  read(fd,buff,sizeof(buff));
  if(sendto(sd,buff,sizeof(buff),0,(struct sockaddr_in *)&cli,sizeof(cli)) >0)
  printf("File has to transferred ....");
 }
 close(sd);
}

Client program

#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<netinet/in.h>
main()
{
 int sd,l;
 char buff[100];
 struct sockaddr_in serv,cli;
 serv.sin_family=AF_INET;
 serv.sin_addr.s_addr=inet_addr("192.168.1.134");
 serv.sin_port=4827;
 sd=socket(AF_INET,SOCK_DGRAM,0);
 if(sd<0)
  printf("\n\t Error in creation of socket");
 else
   printf("\n\t Client socket created");
 printf("\n\t enter the filename to be send :");
    scanf("%s",buff);
 if(sendto(sd,buff,sizeof(buff),0,(struct sockaddr_in *)&serv,sizeof(serv)) >0)
  printf("\n\t Filename to be send successfully");
 else
  printf("\n\t Message Failed ");
 l=sizeof(serv);
 recvfrom(sd,buff,sizeof(buff)+1,0,(struct sockaddr_in *)&serv,&l);
 if(strcmp(buff,"NO"))
 {
  printf("\n\t File Clients form server ");
  printf("%s",buff);
 }
 else
 printf("\n\t File Name does not exits");
 close(sd);
}

OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 6udpftpser.c
 [11ca013@mcalinux network]$ ./a.out 192.168.1.134

         UDP server for file transferring
         server socket created
         Server is ready .......waiting for client .......
         File Name from Client :sample.txt
File has to transferred

CLIENT

[11ca013@mcalinux network]$ cc 6udpftpcli.c
 [11ca013@mcalinux network]$ ./a.out 192.168.1.134

         Client socket created
         enter the filename to be send :sample.txt

         Filename to be send successfully
         File Clients form server
rajivgandhi
hi

how are you

0 Response to "FTP APPLICATION USING UDP"

Post a Comment



Your IP Address is:

Browser: