FTP APPLICATION USING TCP

Server program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc,char **argv[])
{
 int sockfd,i,connfd,t,port;
 char fname[50],cont[100];
 FILE *f;
 struct sockaddr_in servaddr;
 if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
 {
  printf("\n\t Error in socket Creation ");
  exit(0);
 }
printf("Enter the port number");
scanf("%d",&port);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_port=htons(port);
  servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
 if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) <0)
 {
   printf("\n\t Error in Bind");     exit(0);
 }
 if((listen(sockfd,10)) < 0)
 {
   printf("\n\t Error in Listen ");     exit(0);
 }
 for(; ;)
 {
      if((connfd=accept(sockfd,(struct sockaddr*)NULL,NULL)) <0)
   {
     printf("\n\t Error in Accept" );        exit(0);
   }
  recv(connfd,fname,50,0);
  printf("\n\t File is Received : %s \n",fname);
  f=fopen(fname,"r");
  while(!feof(f))
  {
      fread(cont,100,1,f);
      if(!feof(f))
      cont[t]='\0';
      send(connfd,cont,strlen(cont),1);
   }
    printf("\n\t File Content Transferred \n");;
exit(0);
    fclose(f);
   close(connfd);
 }
     }
Client program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(int argc,char **argv[])
{
 int sockfd,i,connfd,t,port;
 char sfname[50],cont[100],dfname[50];
 FILE *fp;
 struct sockaddr_in servaddr;
 if((sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0)
 {
  printf("\n\t Error in socket Creation ");
  exit(0);
 }
  printf("Enter the port number");
  scanf("%d",&port);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_port=htons(port);
  servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))))
 {
   printf("\n\t Error in Connect");
   exit(0);
 }
 printf("\n\n\t Client Module has to run.....");
 printf("\n\t Enter the Source FileName :");
 scanf("%s",sfname);
 printf("\n\t Enter the Destination FileName : ");
 scanf("%s",dfname);
 send(sockfd,sfname,strlen(sfname),0);
 fp=fopen(dfname,"w");
 while(recv(sockfd,cont,100,0) >0)
 {
   fwrite(&cont,strlen(cont),1,fp);
   printf("%s\n",cont);
   strcpy(cont,"\0");
 }
 fclose(fp);
close(sockfd);
}

OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 6tcpftpser.c
[11ca013@mcalinux network]$ ./a.out 192.168.1.134
Enter the port number: 1052

         File is Received: sample.txt

         File Content Transferred

CLIENT

[11ca013@mcalinux network]$ cc 6tcpftpcli.c
[11ca013@mcalinux network]$ ./a.out 192.168.1.134
Enter the port number1052

         Client Module has to run.....
         Enter the Source FileName :sample.txt

         Enter the Destination FileName : gandhi.txt
rajivgandhi
hi

how are you

0 Response to "FTP APPLICATION USING TCP"

Post a Comment



Your IP Address is:

Browser: