KEMBAR78
FTP Client Server | PDF | Port (Computer Networking) | Network Socket
0% found this document useful (0 votes)
80 views4 pages

FTP Client Server

1. The document describes a C++ program to implement a file transfer operation between a client and server. 2. The server creates a TCP socket, binds it to a port, and listens for client requests, while the client creates a socket and connects to the server. 3. The client sends the server a file name, and the server reads the file contents and transfers it to the client.

Uploaded by

click4smok
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views4 pages

FTP Client Server

1. The document describes a C++ program to implement a file transfer operation between a client and server. 2. The server creates a TCP socket, binds it to a port, and listens for client requests, while the client creates a socket and connects to the server. 3. The client sends the server a file name, and the server reads the file contents and transfers it to the client.

Uploaded by

click4smok
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX NO: 5 FTP CLIENT SERVER

.7.2010

AIM: To write a C++ code to implement a file transfer operation between a client and a server.
ALGORITHM:
1.A TCP socket of server is created. The initially empty socket is binded to the ip address and
port number of the server using bind function.
A TCP socket for client is created. It is binded with the ip address and the port number of the
server .
2. The server socket now listens for the client request using listen function.
3. The client connects to the server using the connect function.
4. The client now sends a file name to the server.
5. Server reads the file name and content of the file is transferred to the client.
6. The client receives the file content and stores it in another file.
7. Thus the file transfer operation is executed successfully using client and server.

SERVER:
#include<iostream>
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<string.h>
#define MAX 100
#define len 81
using namespace std;
int main()
{
FILE *fp1;
int socketmain,socketclient,child,port=9998,x1;
struct sockaddr_in serv,client;
socklen_t clientlen;
char str[100],str1[100];
if((socketmain=socket(AF_INET,SOCK_STREAM,0))<0)
{
cout<<"Server cannot open socket";
exit(0);
}
bzero(&serv,sizeof(serv));
bzero(&client,sizeof(client));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(port);
if((bind(socketmain,(struct sockaddr *)&serv,sizeof(serv)))<0)
{
cout<<"Error:Server bind fail";
exit(0);
}
listen(socketmain,5);
if((socketclient=accept(socketmain,0,0))<0)
{
cout<<"Binding with client failed\n";
exit(0);
}
char buf[len];
int msglen;
bzero(buf,len);
if((msglen=recv(socketclient,buf,len,0))<0)
{
cout<<"Error";
exit(1);
}
cout<<"file name receiced";
fp1=fopen(buf,"r");
int len1;
cout<<"\nopened the file";
while((fgets(str,100,fp1))!=NULL)
{
len1=strlen(str);
str[len1]='\0';
send(socketclient,str,len1,0);
}
cout<<"\nSent the data from the file";
close(socketmain);
close(socketclient);
exit(0);
}

Client:
#include<iostream>
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<netdb.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<sys/types.h>
using namespace std;
int main(int argc ,char *argv[])
{
int sockfd,n;
struct sockaddr_in serv;
char str[100];
FILE *fp1;
fp1=fopen("output.txt","w");
if(argc!=3)
{
cout<<"Incorrect parameters";
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
cout<<"Socket not created";
exit(0);
}
char str1[100];
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)
{
cout<<"Conversion error";
}
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))>0)
{
cout<<"connection failed";
}
cout<<"conneted";
cout<<"Enter file name";
cin>>str;
write(sockfd,str,sizeof(str));
while((n=read(sockfd,str1,100))>-1)
{
str1[n]='\0';
fputs(str1,fp1);
}
}

OUTPUT:

SERVER:
C++ serv.cpp
./a.out
File name received
Opened the file

CLIENT:
C++ client.cpp
./a.out 127.0.0.1 9998
Connected Enter the file name : input.txt

Input.txt
S.V.C.E is an esteemed college located in Sriperumbudur, Chennai.
We are proud to be studying in this college.
Output.txt
S.V.C.E is an esteemed college located in Sriperumbudur, Chennai.
We are proud to be studying in this college.

RESULT:
Thus the C++ code to implement the file transfer in client server is executed and output is
obtained.

You might also like