Translate

Monday, October 8, 2012

ME NETWORKS LAB-->FILE TRANSFER PROTOCOL


FILE TRANSFER PROTOCOL

 
SERVER:

 

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

FILE *fp;

int sd,newsd,ser,n,a,cli,pid,bd,port,clilen;

char name[100],fileread[100],fname[100],ch,file[100],rcv[100];

struct sockaddr_in servaddr,cliaddr;

printf("Enter the port address: ");

scanf("%d",&port);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(port);

a=sizeof(servaddr);

bd=bind(sd,(struct sockaddr*)&servaddr,a);

if(bd<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

newsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(newsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

n=recv(newsd,rcv,100,0);

rcv[n]='\0';

fp=fopen(rcv,"r");

if(fp==NULL)

{

send(newsd,"error",5,0);

close(newsd);

}

 

else

{

while(fgets(fileread,sizeof(fileread),fp))

{

if(send(newsd,fileread,sizeof(fileread),0)<0)

{

printf("Can't send\n");

}

sleep(1);

}

if(!fgets(fileread,sizeof(fileread),fp))

{

send(newsd,"completed",999999999,0);

}

return(0);

}

}

 

 

 


 


 


 


 


 


 


 

 

 

 

 

 

 

 

 

 

 

 

 

 


CLIENT


#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

main()

{

FILE *fp;

int csd,n,ser,s,cli,cport,newsd;

char name[100],rcvmsg[100],rcvg[100],fname[100];

struct sockaddr_in servaddr;

printf("Enter the port");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

{

printf("Error...");

exit(0);

}

else

printf("Socket is Created...\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Error in Connection...\n");

else

printf("Connected...\n");

printf("Enter the existing file name: ");

scanf("%s",name);

printf("\nEnter the new filename: ");

scanf("%s",fname);

fp=fopen(fname,"w");

send(csd,name,sizeof(name),0);

while(1)

{

s=recv(csd,rcvg,100,0);

rcvg[s]='\0';

if(strcmp(rcvg,"error")==0)

printf("File is not Available...\n");

if(strcmp(rcvg,"completed")==0) {

printf("file is transferred...\n");

fclose(fp);

close(csd);

break;

}

else

fputs(rcvg,stdout);

fprintf(fp,"%s",rcvg);

}

}

 

 

 


OUTPUT :

 

[mecse8@localhost mecse8]$ cc files1.c

[mecse8@localhost mecse8]$ ./a.out

 

Enter the port6789

Socket is Created

 

Binded

Accepted

 

[mecse8@localhost mecse8]$ cc filec1.c

[mecse8@localhost mecse8]$ ./a.out

Enter the port6789

Socket is Created...

Connected...

Enter the existing file name: a

 

Enter the new filename: aa

hai

 

file is transferred...

 

No comments:

Post a Comment