File Transfer FTP Using TCP | source code in C Network Programming

To write a program to a created file from the server to the client.
File transfer TCP Algorithm
Server side Filer Transfer TCP Algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure for the socket.
STEP 3: Create a socket using socket functions
STEP 4: The socket is binded at the specified port.
STEP 5: Using the object the port and address are declared.
STEP 6: After the binding is executed the file is specified.File Transfer Using TCP in C Network Programming
STEP 7: Then the file is specified.
STEP 8: Execute the client program.
Client File Transfer TCP programming
Algorithm

STEP 1: Start the program.
STEP 2: Declare the variables and structure.
STEP 3: Socket is created and connects function is executed.
STEP 4: If the connection is successful then server sends the message.
STEP 5: The file name that is to be transferred is specified in the client side.
STEP 6: The contents of the file is verified from the server side.
STEP 7: Stop the program
Server Side source code programming
#include<string.h>
#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<net/if_arp.h>
.int main()
{
int sd,b,cd;
struct fname[50],op[1000];
struct sockaddr_in caddr,saddr;
FILE *fp;
socklen_t clen=sizeof(caddr);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd!=-1)
printf(“socket is created”);
else
printf(“socket is not created”);
saddr.sin_family=AF_INET;
saddr.sin_port=htons(2500);
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
b=bind(sd,(struct sockaddr*)&saddr,sizeof(saddr));
if(b==0)
printf(“binded successfully”);
else
printf(“binding failed’);
listen(sd,5);
cd=accept(sd,(struct sockaddr*)&caddr,&clen);
recv(cd,fname,sizeof(fnmae),0);
fp=open(fname,”w”);
fwrite(op,strlen(op),1,fp);
printf(“the file has been transferred”);
close(fd);
close(cd);
fclose(fp);
return 0;
}
Client Side Program:
#include<string.h>
#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<net/if_arp.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb,h>
int main()
{
int sd,c,s;
char fname[50],sip[25],op[1000];
struct sockaddr_in caddr;
struct hostent *he;
FILE *fp;
printf(‘enter the server ip address”);
scanf(“%s”,sip);
he=gethostbyname(sip0;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd!=1)
printf(“socket created”);
else
printf(“socket is not created’);
caddr.sin_family=AF_INET;
caddr.sin_port=htons(2500);
caddr.sin_addr=*((struct in_addr*)he->h_addr);
c=connect(sd,(struct sockaddr*)&caddr,sizeof(caddr));
if(c==0)
printf(“connected to server”);
else
printf(“connection failed”);
printf(“enter the file name’);
scanf(“%s”,fname);
send(sd,fname,sizeof(fname),0);
fp=fopen(fname,”r”);
fopen(op,1000,1,fp);
send(sd,op,sizeof(op),0);
fclose(fp);
close(sd);
return 0;
}
INPUT OUTPUT CLIENT CS1305 Network Lab
Enter the server ip address 127.0.0.1
Socket created
Connected to the server
Enter the file name cli.txt
cli.txt
Network programming lab
B.tech I.T
Third year
06 sem
SERVER CS1305 Network Lab
Socket is created
Binded successfully
Enter the file name ser.txt
The file has been transferred
Ser.txt
Network programming lab
B.tech I.T
Third year
06 sem
RESULT Thus the program to transfer a file from the client to the server is
executed and verified. Free Download C program.

Related post