Click here to Skip to main content
Click here to Skip to main content

FTP Client Class

By , 8 Dec 2012
 

Introduction

CFTPClient is a class to encapsulate the FTP protocol. I have tried to implement it as platform independent. For the purpose of communication, I have used the classes CBlockingSocket, CSockAddr, ... from David J. Kruglinski's "Inside Visual C++". These classes are only small wrappers for the sockets-API. Further, I have used a smart pointer-implementation from Scott Meyers "Effective C++, More Effective C++, Effective STL". The implementation of the logon-sequence (with Firewall support) was published in an article on CodeGuru by Phil Anderson. The code for the parsing of different FTP LIST responses is taken from D. J. Bernstein's (parsing code). I only wrapped the C code in a class. I haven't tested the code on other platforms, but I think with little modifications it would compile and run smoothly.

The main features are:

  • not based on MFC-sockets,
  • not using other MFC-stuffs like CString (uses STL),
  • supports Firewalls,
  • supports resuming,
  • supports file eXchange Protocol (FXP) - uses FTP to transfer data directly from one remote server to another (servers must support this feature),
  • testet under Windows with Visual Studio 2008,
  • testet under Linux (Suse 11.4) with Qt,
  • smart pointer implementation can be easily replaced with boost::shared_ptr or std::shared_ptr by defining USE_BOOST_SMART_PTR or USE_STD_SMART_PTR,
  • parser which parses the output of the LIST command can be replaced by implementing the interface "IFileListParser",
  • can be easily extended.

The example shows how easy it is to use this class. With a few lines of code you can log the communication or visualize file transfers. Notice: The example is not a fully functional FTP-client-application. The example application is only for Windows platforms.

Background

The official specification of the File Transfer Protocol (FTP) is the RFC 959. Most of the documentation in my code are taken from this RFC.

Using the code

There are a lot of classes. But most of them are just simple "datatypes". The most important ones are the following:

  • CFTPClient
    The heart of the application. It accepts a CLogonInfo object. Handles the complete communication with the FTP-server like:
    • get directory listing,
    • download/upload files,
    • delete directories/files,
    • walk through directory-tree,
    • passive mode,
    • ...
  • CLogonInfo
    A simple data structure for logon information, such as host, username, password, firewall, ...
  • CFTPClient::IFileListParser
    Interface for defining a parser class which can be set in the CFTPClient class for parsing the output of the LIST command.

  • CFTPClient::ITransferNotification
    Implementations of this interface can be used in the Download and Upload methods for controlling the byte streams which are be downloaded/uploaded. This can be used for example to download a file only into memory instead of a local file (see class COutputStream).

  • CFTPClient::CNotification
    The base class for notification mechanism. The class which derives from CFTPClient::CNotifaction can be attached to the CFTPClient class as an observer. The CFTPClient object notifies all the attached observers about the various actions (see example application):

    void TestFTP()
    {
       nsFTP::CFTPClient ftpClient;
       nsFTP::CLogonInfo logonInfo(_T("localhost"), 21, _T("anonymous"),
                                               _T("<a href="mailto:anonymous@user.com">anonymous@user.com"));
    
       // connect to server
       ftpClient.Login(logonInfo);
    
       // get directory listing
       nsFTP::TFTPFileStatusShPtrVec list;
       ftpClient.List(_T("/"), list);
    
       // iterate listing
       for( nsFTP::TFTPFileStatusShPtrVec::iterator it=list.begin();
                                                it!=list.end(); ++it )
           TRACE(_T("\n%s"), (*it)->Name().c_str());
    
       // do file operations
       ftpClient.DownloadFile(_T("/pub/test.txt"), _T("c:\\temp\\test.txt"));
    
       ftpClient.UploadFile(_T("c:\\temp\\test.txt"), _T("/upload/test.txt"));
    
       ftpClient.Rename(_T("/upload/test.txt"), _T("/upload/NewName.txt"));
    
       ftpClient.Delete(_T("/upload/NewName.txt"));
    
       // disconnect
       ftpClient.Logout();
    }
    
    void TestFXP()
    {
       nsFTP::CFTPClient ftpClientSource;
       nsFTP::CLogonInfo logonInfoSource(_T("sourceftpserver"), 21, _T("anonymous"),
                                                           _T("<a href="mailto:anonymous@user.com">anonymous@user.com"));
    
       nsFTP::CFTPClient ftpClientTarget;
       nsFTP::CLogonInfo logonInfoTarget(_T("targetftpserver"), 21, _T("anonymous"),
                                                           _T("<a href="mailto:anonymous@user.com">anonymous@user.com"));
    
       // connect to server
       ftpClientSource.Login(logonInfoSource);
       ftpClientTarget.Login(logonInfoTarget);
    
    
       // do file operations
       nsFTP::CFTPClient::TransferFile(ftpClientSource, _T("/file.txt"),
                                       ftpClientTarget, _T("/newFile.txt"));
    
    
       // disconnect
       ftpClientTarget.Logout();
       ftpClientSource.Logout();
    }
    
    
    void TestDownloadAsciiFileIntoTextBuffer()
    {
       nsFTP::CFTPClient ftpClientSource;
       nsFTP::CLogonInfo logonInfoSource(_T("sourceftpserver"), 21, _T("anonymous"),
                                                           _T("<a href="mailto:anonymous@user.com">anonymous@user.com</a>"));
    
       // connect to server
       ftpClientSource.Login(logonInfoSource);
    
       nsFTP::COutputStream outputStream(_T("\r\n"), _T("Example"));
    
       // do file operations
       ftpClientSource.DownloadFile(_T("/file.txt"), outputStream,
                                    nsFTP::CRepresentation(nsFTP::CType::ASCII()));
    
       tstring output = outputStream.GetBuffer();
    
       // disconnect
       ftpClientSource.Logout();
    }
    
    

History

  • 2004-10-25 - First public release.
  • 2005-12-04 - Version 1.1
    • Some interfaces changed (e.g. CNotification).
    • Bug in OpenPassiveDataConnection removed: SendCommand was called before data connection was established.
    • Bugs in GetSingleResponseLine removed:
      • Infinite loop if the response line doesn't end with CRLF.
      • Return value of std:string->find must be checked against npos.
    • Now runs in Unicode.
    • Streams removed.
    • Explicit detaching of observers are not necessary anymore.
    • ExecuteDatachannelCommand now accepts an ITransferNotification object. Through this concept there is no need to write the received files to a file. For example, the bytes can be written only in memory or another TCP stream.
    • Added an interface for the blocking socket (IBlockingSocket). Therefore it is possible to exchange the socket implementation, e.g. for writing unit tests (by simulating a specific scenario of a FTP communication).
    • Replaced the magic numbers concerning the reply codes with a class.
    • New example added. A console application created with Bloodshed Dev-C++. It is only a small application which should demonstrate the use of the classes in a non Microsoft environment.
  • 2012-12-02 - Version 2.0
    • fixed some bugs
    • introduced more "data types" for more secure interfaces
    • code modified so it also runs under Linux
    • support for file eXchange Protocol (FXP)

What will be done next

  • Example application with Linux GNU-C++.
  • New features for FTP client class (for example: copy and delete recursively).
  • Unit tests.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

otom
Software Developer (Senior)
Germany Germany
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCompile errors vc++ 2010 Pinmemberruy ruiz13-Jun-13 8:42 
i got these errors in visual c++ 2010 Express
ftpclient\Definements.h(127): error C3254: 'nsHelper::CError'
ftpclient\Definements.h(127): error C2838: 'GetLastWin32Error'
Questionexample? Pinmemberganfeng20034-Jun-13 17:02 
Sir, where is the console example? I download the demo project but can not find the example.
ganfeng

Questionconsole example? Pinmemberganfeng20034-Jun-13 17:00 
Sir, where is the console example? I download the demo project but can not find the example.
ganfeng

AnswerRe: console example? Pinmemberotom4-Jun-13 18:53 
GeneralMy vote of 5 Pinmemberganfeng20034-Jun-13 16:56 
great.
Suggestionone more adjustment for Linux PinmemberMember 860761216-Apr-13 9:57 
changes in gcc 4.7. require unistd.h to be included. In BlockingSocket.h line number 34
#elif defined(unix)
   #include <sys/socket.h>
   #include <unistd.h>
   #include <arpa/inet.h>   // needed for inet_ntoa and inet_addr
   #include <netdb.h>       // needed for gethostbyname and gethostbyaddr
   #include <errno.h>
   #include <stddef.h>
#endif

GeneralWorks like a charm Pinmemberrob_toutant7-Mar-13 21:54 
Needed to be able to sync an ftp folder to a local folder and your code worked right out of the box
 
Well written ( unlike my spaghetti code ) and no bugs that I can see
 
Thank you for posting the code and spending the time to document
Questionsome small adjustments for Linux Pinmembergrouby7521-Feb-13 2:19 
if compiled with -std=c++0x (c++11), you have a warning:
FTPClient.cpp:313:78: warning: 'auto_ptr' is deprecated
 
and an error:
BlockingSocket.cpp need: #include stddef.h for ptrdiff_t (line 407)
Questionhow to upload large File (>4GB) Pinmembergcpony26-Dec-12 22:48 
how to use this class when I want to upload large file which than 4GB,
 
thanks
yes

GeneralMy vote of 5 PinmemberMihai MOGA14-Dec-12 6:11 
This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up once again.
GeneralMy vote of 5 Pinmemberjustdownloads13-Dec-12 0:46 
thanks a lot for this great article !
Questionis there resource leak? Pinmemberhaifeng21714-Aug-12 4:33 
hi, i found a problem when i use the code as follow
nsFTP::CFTPClient   m_LocalFTPClient;
nsFTP::CLogonInfo LogonInfo;
 
if (!m_LocalFTPClient.IsConnected())
{
   LogonInfo.SetHost(szSVRIP, 21, szUserName, szPassWD, static_cast<LPCTSTR>(""));
   if (m_LocalFTPClient.Login(LogonInfo))
   {
      nsFTP::TSpFTPFileStatusVector list;
      m_LocalFTPClient.List("/", list);
   }
}
 
when i comment out "m_LocalFTPClient.List("/", list);" there is no resource leak,otherwise
the process's memory is increased by 4k once the code is invoked, could anyone come aross
this problem?
Questiondownload files with filename matching Pinmembermallouna4-Jul-12 22:28 
how to download all files with filename matching using c++ from an ftp server
thanks
SuggestionGreat Library small bug for File Modification Time PinmemberLefterisg30-May-12 6:03 
In method
 
int CFTPClient::FileModificationTime(const tstring& strPath, tstring& strModificationTime) const
 
Change line 1676 (iPos > -1) to the below
 
if( iPos != tstring::npos ) // instead of (iPos > -1)
 
Leaving to (iPos > -1) is not working properly with the compiler I use, it always fails. Since -1 for unsigned is also the biggest possible number.
QuestionChange protocol Type Pinmemberjazaman14-May-12 19:31 
In BlockingSocket.cpp function
void CBlockingSocket::Create(int nType /* = SOCK_STREAM */)
 
Had to change
 
if( (m_hSocket=socket(AF_INET, nType, 0)
 
to
 
if( (m_hSocket=socket(AF_INET, nType, IPPROTO_TCP)
 
Otherwise the socket never received a response.
QuestionCan't compile the code PinmemberHooch18012-Feb-12 4:35 
Hello.
I can't compile the code.
I'm getting those errors.
 
Cannot open include file: stdafx.h
 
This error occures in those files:
BlockingSocket.cpp line 8
FTPClient.cpp line 49
FTPDataTypes line 16
FTPFIleState.cpp line 16
FTPListPare line 29
AnswerRe: Can't compile the code PinmemberFrank Heimes11-Mar-12 23:09 
QuestionCalling of ::List() does not react at all PinmemberMember 440367423-Dec-11 5:00 
Hi otom,
I try to retrieve all folders and files on the ftp-server and an instance of CFTPClient runs in a thread. but i get always a problem when excuting List(), just like this:
 
nsFTP::TSpFTPFileStatusVector list;
if (!client.List(url.toStdWString(), list)) { .... }
 
so the thread is blocked... as i debugged, i saw that occurred in the function
CFTPClient::OpenActiveDataConnection(...) {
...
if( !apSckServer->Accept(sckDataConnection, sockAddrTemp) ) {
...
}
and in the function
bool CBlockingSocket::Accept(...) {
...
pConnect->m_hSocket = accept(m_hSocket, psa, &nLengthAddr);
...
}
 
Calling of accept(m_hSocket, psa, &nLengthAddr) returns never!
this phenomenon occurs at some subfolders on the server that i have access to them.
 
any idea? thank you
 
aurora
AnswerRe: Calling of ::List() does not react at all Pinmemberotom23-Dec-11 6:00 
Have you more than one CFTPClient connection to the server open? The list command opens a data connection on the server. Sometimes servers do not allow to open more than one data connection from a client at the same time.
Maybe there is a bug in the posted version. I am going to release a new version soon.
GeneralRe: Calling of ::List() does not react at all [modified] PinmemberMember 440367423-Dec-11 6:54 
GeneralRe: Calling of ::List() does not react at all PinmemberMember 863475024-Apr-13 1:19 
BugSending/Receiving data using select() PinmemberKHSIEMENS21-Dec-11 21:56 
I got the FTPClient Code compiled for Linux, but first it didn't work. The problem was,
that for some select() calls in the code, the first parameter nfds was set to 0. This doesn't
matter under Windows, because this parameter is ignored.
Under Linux, the first parameter in select() must be set to the highest file descriptor + 1
 
Regards,
KH
QuestionFtp Onresponse event [modified] Pinmemberpippo pioppo20-Dec-11 0:00 
Hi,
I'm testing your demo project, but when I run it, I don't obtain the logging response. Using a break point in function
void CFTPProtocolOutput::OnResponse(const tstring& strResponse)
on first line, the execution of the application is not stopped. Of course response string is not logging.
 
Can you help me to understand why this event occurred?
 
10x
 
===============================================================
Ok,
I found the solution to what was previously reported.
In the example you have made
 
void CFTPProtocolOutput: OnResponse (const & tstring strResponse)
 
but evidently, after this, you have modified the parent object
and then you must override
 
void CFTPProtocolOutput: OnResponse (const CReply & reply)
 
and working properly.
 
Because I have written, I must point out that in the function
 
void CFTPProtocolOutput: OnSendCommand (const tstring & strCommand)
 
you have entered the instruction
 
if (strCommand.length ()> 4 & & strCommand.substr (5) == _T ("PASS"))
 
but, at least with VS2008, this does not work. I corrected it in
 
if (strCommand.length ()> 4 & & strCommand.substr (0, 5) == _T ("PASS"))
 
and it's work fine.
 
Excellent work,
10x
Sandro


modified 20-Dec-11 8:48am.

QuestionLicence?? Pinmemberpippo pioppo8-Dec-11 23:45 
Hi,
I'll want to know what's kind of licence is applied to this project. Can I use it for free in commercial and/or open source code?
 
10x
Sandro
Sandro

AnswerRe: Licence?? Pinmemberotom11-Dec-11 6:07 
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 9 Dec 2012
Article Copyright 2004 by otom
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid