© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

C Program To Implement File Transfer Protocol:

//Server Program: #include #include #include #include #include #include #include #include #include #include #define MAX 100; int main () { int listenfd,connfd,n,nl; struct sockaddr_in serv; char str1 [100],str2[100],fname[20],fname1[20],s[2], int port=9999; FILE*f1*f2; listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&serv,sizeof(serv); serv.sin_family=AF_INET; serv.sin_addr.s_addr=htonl(INADDR_ANY); serv.sin_port=htons(port); bind(listenfd,(struct sockaddr*)&serv,sizeof(serv)); "serftp4.cpp"54L, 1518C listen(listenfd,5); for(;;) { connfd=accept(listenfd,(struct sockaddr*)NULL,NULL); printf("\nClient requesting"); n=read(connfd,fname,20); fname(n)='\0'; printf("\n Received:%s\n",fname); f1=fopen(fname)."r"); strcpy(s,"ab"); write(connfd,s,strlen(s)); n1=read(connfd,fname1,20); fname1[n1]=;\0'; printf("stored in:%s\n"'fname 1); f2=fopen(fname1,"w"); while(!feof(f1)) { fgets(str1,50f1); write(connfd,str,1,strlen(str1)); n=read(connfd,str,2,100); str2[n]='\0'; fputs(str2,f2); } fclose(f1); fclose(f2); close(connfd); } return 0; } //Client Program: #include #include #include #include #include #include #include #include #include #include #define MAX 100; main(int argc,char**argv) { int sockfd,n,connfd; char str[100],str1[100],s[2]; struct sockaddr_in serv; if(argc!=5) { exit(0); } if(sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { printf("\n Error ! Socket not created...\n"); exit (0); } bzero(&serv,sizeof(serv)); serv.sin_family=AF_INET; serv.sin_port=htons(atoi(argv[2])); if(inet_pton(AF_INET,argv[1],&serv.sin_addr)<=0) { printf("\n error in conversion of IP address from string to num\n"), exit(0); } if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0) { printf("\n Error!Conx not established...\n); exit(0); } printf("\n connected...sending file name%s\n",argv[3]); write(sockfd,argv[],strlen(argv[3])); read(sockfd,s,3); write(sockfd,argv[4],strlen(argv[4])); str1[0]='\0'; while((n=read(sockfd,str,100))>0) { str[n]='\0'; printf("%s\n",str); write(sockfd,str,strlen(str)); } if(n<0) printf("\n Read error...\n"); exit (0); }

SAMPLE INPUT OUTPUT:

Server Side:

[cseb17@localhost cseb 17]$ cc trj3server.c
[cseb17@localhost cseb 17]$.a/out
Client requesting
Received:new.c
Stored in : thanga.c

Client side:

[cseb17@localhost cseb 17]$cc trj3client.c
[cseb17@localhost cseb 17]$./a.out 127.0.0.1 6142 new.c.thanga.c

connected...sending filename new.c
hai how are you?

 
SLogix Student Projects
bottom