Click here to Skip to main content
13,296,377 members (57,382 online)
Click here to Skip to main content
Add your own
alternative version


236 bookmarked
Posted 27 Oct 2004

FTP Client Class

, 8 Dec 2012
Rate this:
Please Sign up or sign in to vote.
A non-MFC class to encapsulate the FTP protocol.


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.


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="">"));
       // connect to server
       // 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"));
       // disconnect
    void TestFXP()
       nsFTP::CFTPClient ftpClientSource;
       nsFTP::CLogonInfo logonInfoSource(_T("sourceftpserver"), 21, _T("anonymous"),
                                                           _T("<a href="">"));
       nsFTP::CFTPClient ftpClientTarget;
       nsFTP::CLogonInfo logonInfoTarget(_T("targetftpserver"), 21, _T("anonymous"),
                                                           _T("<a href="">"));
       // connect to server
       // do file operations
       nsFTP::CFTPClient::TransferFile(ftpClientSource, _T("/file.txt"),
                                       ftpClientTarget, _T("/newFile.txt"));
       // disconnect
    void TestDownloadAsciiFileIntoTextBuffer()
       nsFTP::CFTPClient ftpClientSource;
       nsFTP::CLogonInfo logonInfoSource(_T("sourceftpserver"), 21, _T("anonymous"),
                                                           _T("<a href=""></a>"));
       // connect to server
       nsFTP::COutputStream outputStream(_T("\r\n"), _T("Example"));
       // do file operations
       ftpClientSource.DownloadFile(_T("/file.txt"), outputStream,
       tstring output = outputStream.GetBuffer();
       // disconnect


  • 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.


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


About the Author

Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionGetting Pin
Saviovt1021-Nov-11 0:12
memberSaviovt1021-Nov-11 0:12 
AnswerRe: Getting Pin
otom11-Dec-11 7:13
memberotom11-Dec-11 7:13 
QuestionReg: Writing a stream over FTPSession Pin
Sivaram Kannan3-Aug-11 5:10
memberSivaram Kannan3-Aug-11 5:10 
QuestionRegarding secure FTP Pin
Saviovt1012-Jul-11 20:51
memberSaviovt1012-Jul-11 20:51 
AnswerRe: Regarding secure FTP Pin
otom12-Jul-11 22:03
memberotom12-Jul-11 22:03 
GeneralWSAStartup Problem Pin
wolf30572-Jan-11 15:58
memberwolf30572-Jan-11 15:58 
Generalerror when download file to strLocalFile which contains chinese word Pin
Seshaal4-May-10 18:36
memberSeshaal4-May-10 18:36 
GeneralAbort takes a lot of time! Pin
SpringMVC19-Mar-10 1:43
memberSpringMVC19-Mar-10 1:43 
QuestionAny one can help me to download file on ftp server recursively by using these codes? Pin
songge17-Dec-09 22:28
membersongge17-Dec-09 22:28 
GeneralCompilling and running under VC++ 2002 Pin
Leandro T C Melo14-May-09 5:28
memberLeandro T C Melo14-May-09 5:28 
GeneralRe: Compilling and running under VC++ 2002 Pin
JackSimmons1-Jun-09 14:17
memberJackSimmons1-Jun-09 14:17 
GeneralBug in CFTPClient::UploadFile Pin
Gluck24-Apr-09 0:12
memberGluck24-Apr-09 0:12 
General[Message Deleted] Pin
it.ragester2-Apr-09 22:58
memberit.ragester2-Apr-09 22:58 
GeneralA very easy port to Linux Pin
Member 455517230-Sep-08 7:14
memberMember 455517230-Sep-08 7:14 
Very nice. Thank you. most of the problems i had were with the windows data types (USHORT, DWORD, SOCKADDR, etc.). i created another header file "ftp.h" to hold the typedefs and a few other definitions. also had to change the calls to "select".

still need to write a program to test all the functions, but from what i have seen it seems to work quite well.

thank you.
Charles Wright
GeneralA comfortable FTP class in .NET Pin
Elmue27-Aug-08 13:18
memberElmue27-Aug-08 13:18 
QuestionHas anyone got this to compile with VC6? Pin
pscholl21-Jul-08 6:28
memberpscholl21-Jul-08 6:28 
AnswerRe: Has anyone got this to compile with VC6? Pin
DamDaDum28-Oct-08 4:26
memberDamDaDum28-Oct-08 4:26 
General... Pin
AHTOXA18-Jun-08 3:41
memberAHTOXA18-Jun-08 3:41 
GeneralSOCKET handle(Control Link which listen to accept data link) unclosed when calling Upload Pin
Member 458698613-Apr-08 23:10
memberMember 458698613-Apr-08 23:10 
GeneralRe: SOCKET handle(Control Link which listen to accept data link) unclosed when calling Upload Pin
otom14-Apr-08 8:50
memberotom14-Apr-08 8:50 
GeneralRe: SOCKET handle(Control Link which listen to accept data link) unclosed when calling Upload Pin
lidongmao31-Jan-10 15:20
memberlidongmao31-Jan-10 15:20 
Questionwhy the port is not released when I use port mode? Pin
98863018-Dec-07 19:07
member98863018-Dec-07 19:07 
GeneralRe: why the port is not released when I use port mode? Pin
otom27-Jan-08 20:22
memberotom27-Jan-08 20:22 
GeneralMFC compatability Pin
triplebit9-Nov-07 5:18
membertriplebit9-Nov-07 5:18 
GeneralReply error after upload/download files Pin
calvfoo1-Nov-07 1:20
membercalvfoo1-Nov-07 1:20 
QuestionA program about UpLoadFile Pin
lihe8765440317-Oct-07 20:12
memberlihe8765440317-Oct-07 20:12 
AnswerRe: A program about UpLoadFile Pin
otom8-Oct-07 8:39
memberotom8-Oct-07 8:39 
GeneralRe: A program about UpLoadFile Pin
lihe8765440318-Oct-07 17:09
memberlihe8765440318-Oct-07 17:09 
GeneralRe: A program about UpLoadFile Pin
otom16-Oct-07 20:37
memberotom16-Oct-07 20:37 
GeneralRe: A program about UpLoadFile Pin
lihe87654403117-Oct-07 22:32
memberlihe87654403117-Oct-07 22:32 
GeneralRe: A program about UpLoadFile Pin
otom18-Oct-07 10:14
memberotom18-Oct-07 10:14 
GeneralRe: A program about UpLoadFile Pin
lihe87654403122-Oct-07 21:19
memberlihe87654403122-Oct-07 21:19 
Questionlinkage errors Pin
gabriellopes19-Jun-07 15:35
membergabriellopes19-Jun-07 15:35 
AnswerRe: linkage errors Pin
Lushan11119-Jun-07 20:40
memberLushan11119-Jun-07 20:40 
GeneralRe: linkage errors Pin
gmlopes20-Jun-07 4:08
membergmlopes20-Jun-07 4:08 
GeneralRe: linkage errors Pin
gmlopes20-Jun-07 7:52
membergmlopes20-Jun-07 7:52 
QuestionBug on connection break? Pin
Lushan11114-Jun-07 3:17
memberLushan11114-Jun-07 3:17 
AnswerRe: Bug on connection break? Pin
otom14-Jun-07 10:25
memberotom14-Jun-07 10:25 
GeneralRe: Bug on connection break? [modified] Pin
Lushan11114-Jun-07 17:11
memberLushan11114-Jun-07 17:11 
GeneralRe: Bug on connection break? [modified] Pin
Lushan11114-Jun-07 21:46
memberLushan11114-Jun-07 21:46 
GeneralRe: Bug on connection break? Pin
otom17-Jun-07 21:29
memberotom17-Jun-07 21:29 
GeneralRe: Bug on connection break? Pin
Lushan11120-Jun-07 17:02
memberLushan11120-Jun-07 17:02 
GeneralUnix port Pin
MarcoNedwig6-May-07 0:27
memberMarcoNedwig6-May-07 0:27 
GeneralWinsock library Pin
wous4-Mar-07 11:59
memberwous4-Mar-07 11:59 
Answer[TIP] Linking in DEV C++ Pin
RAD CPP16-Aug-07 12:19
memberRAD CPP16-Aug-07 12:19 
GeneralMinor bug - GetPeerAddr declaration missing in IBlockingSocket Pin
toesinsock17-Oct-06 20:14
membertoesinsock17-Oct-06 20:14 
Questionhow can it work in the No.21 port? Pin
bobcow14-Oct-06 2:45
memberbobcow14-Oct-06 2:45 
AnswerRe: how can it work in the No.21 port? Pin
Lushan11119-Jun-07 20:37
memberLushan11119-Jun-07 20:37 
GeneralCompile Problems Pin
Dark_Guru4-Sep-06 22:36
memberDark_Guru4-Sep-06 22:36 
GeneralProblems Including your class (im a noob =[ ) Pin
RedSpyder29-Jun-06 12:05
memberRedSpyder29-Jun-06 12:05 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 9 Dec 2012
Article Copyright 2004 by otom
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid