FILE TRANSFER PROTOCOL
DATE:
FILE TRANSFER PROTOCOL
EXNO:
AIM:
To write a C program to transfer a file using TCP.
ALGORITHM:
SERVER:
Step 1:
Step 2:
.
Step 3:
.
Step 4:
.
Step 5:
Step 6:
.
Step 7:
Step 8:
Start the program.
Create an unnamed socket for the server using parameters AF_INET as domain
and SOCK_STREAM as type
Get the server port number register the host address to system by using bind()
system call in the server side
Create a connection queue and wait for clients using listen() system call with the
number of clients as parameter
Create a child process using fork() system call.
If the process identification number is equal to zero accept the connection using
accept() system call when the client request for the connection
If pid is not equal to zero then exit the process.
Stop the program execution.
CLIENT:
Step 1:
Step 2:
.
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Start the program.
Create an unnamed socket for the server using parameters AF_INET as domain
and SOCK_STREAM.
Get the client port number.
Now connect the socket to the server using connect() system call.
Enter the filename.
The file is transferred from client to server using send() function.
Print the contents of the file in a new file.
Stop the program.
PROGRAM:
FILE TRANSFER PROTOCOL
SERVER:
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{
FILE *fp;
int sd,sd2,nsd,clilen,sport,len,n,port;
char name[20],fname[20],file[20],fileread[20],ch,rcv[100];
struct sockaddr_in servaddr, cliaddr;
printf("Enter server ports\n");
scanf("%d",&sport);
printf("%d\n",sport);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("cant 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(sport);
sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(sd2<0)
FILE TRANSFER PROTOCOL
printf("cant bind");
else
printf("Binded\n");
listen(sd,5);
clilen=sizeof(cliaddr);
nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
if(nsd<0)
printf("cant accept");
else
printf("Accepted");
n=recv(nsd,rcv,50,0);
rcv[n]='\0';
fp=fopen(rcv,"r");
if(fp==NULL)
{
send(nsd,"error",5,0);
close(nsd);
}
else
{while(fgets(fileread,sizeof(fileread),fp))
{if(send(nsd,fileread,sizeof(fileread),0)<0)
{
printf("cant send");
}
FILE TRANSFER PROTOCOL
sleep(1);
}if(!fgets(fileread,sizeof(fileread),fp))
{send(nsd,"completed",999999999,0);
}return(0);
}}
CLIENT:
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{
FILE *fp;
int csd,cport,len,s;
char name[20],rcvmsg[20],rcvg[20],fname[100];
struct sockaddr_in servaddr;
printf("enter the port\n");
scanf("%d",&cport);
printf("%d\n",cport);
csd=socket(AF_INET,SOCK_STREAM,0);
if(csd<0)
printf("cant create\n");
else
printf("created");
FILE TRANSFER PROTOCOL
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("cant connect");
else
printf("connected");
printf("Enter the existing file name:");
scanf("%s",name);
printf("Enter the new file name:");
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 does not exist \n");
if(strcmp(rcvg,"completed")==0) }
printf("File transfered");
fclose(fp);
close(csd);
break; }
else
FILE TRANSFER PROTOCOL
fputs(rcvg,stdout);
fprintf(fp,"%s",rcvg); }}
OUTPUT:
SERVER:
[rit@localhost~]$ cc serverftp.c
[rit@localhost~]$ ./[Link]
Enter the server port
4563
4563
Socket is created
Binded
Accepted
CLIENT:
[rit@localhost~]$ cc clientftp.c
[rit@localhost~]$ ./[Link]
Enter the port
4563
4563
Socket is created
Connected
Enter the existing filename: abc
Enter the new filename
: xyz
FILE TRANSFER PROTOCOL
Hello friends
Bye
File transferred
RESULT:
Thus the C program for file transfer using TCP is implemented and executed
successfully.