Click here to Skip to main content
12,303,348 members (79,483 online)
Click here to Skip to main content
Add your own
alternative version


230 bookmarked

FTP Client Class

, 8 Dec 2012 CPOL
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

Praisenice lib Pin
louisejackie15-May-16 17:39
memberlouisejackie15-May-16 17:39 
BugUpload does not work. Do not spend your time on it Pin
Member 114535452-Sep-15 10:13
memberMember 114535452-Sep-15 10:13 
GeneralRe: Upload does not work. Do not spend your time on it Pin
Member 176990929-May-16 20:42
memberMember 176990929-May-16 20:42 
Generalnice lib Pin
NeverForever6-Aug-15 16:14
memberNeverForever6-Aug-15 16:14 
GeneralPerfect Pin
Member 1130039212-Dec-14 4:08
memberMember 1130039212-Dec-14 4:08 
QuestionFile modification time Pin
mrbogus2731-Oct-14 1:38
membermrbogus2731-Oct-14 1:38 
Questionupload zip file bugs Pin
lion_11722-Aug-14 17:24
memberlion_11722-Aug-14 17:24 
AnswerRe: upload zip file bugs Pin
ztana23-Dec-14 23:02
memberztana23-Dec-14 23:02 
QuestionAbort Pin
member 626847327-Mar-14 12:06
membermember 626847327-Mar-14 12:06 
QuestionProblem to upload a file by FTP Pin
Member 1067597618-Mar-14 1:14
memberMember 1067597618-Mar-14 1:14 
AnswerRe: Problem to upload a file by FTP Pin
Ashish Tyagi 4025-Mar-14 23:10
memberAshish Tyagi 4025-Mar-14 23:10 
GeneralRe: Problem to upload a file by FTP Pin
Member 1067597625-Mar-14 23:15
memberMember 1067597625-Mar-14 23:15 
GeneralRe: Problem to upload a file by FTP Pin
Member 1067597626-Mar-14 1:19
memberMember 1067597626-Mar-14 1:19 
GeneralRe: Problem to upload a file by FTP Pin
Ashish Tyagi 4026-Mar-14 6:01
memberAshish Tyagi 4026-Mar-14 6:01 
QuestionIssue trying to build in QTCreator on Ubuntu 12 Pin
Neriam18-Feb-14 6:18
memberNeriam18-Feb-14 6:18 
AnswerRe: Issue trying to build in QTCreator on Ubuntu 12 Pin
omid1205-Nov-14 21:35
memberomid1205-Nov-14 21:35 
QuestionDoes it append to a file? Pin
KiranV278-Aug-13 5:43
memberKiranV278-Aug-13 5:43 
QuestionProblem with connect Pin
Martin Perry26-Jul-13 8:12
memberMartin Perry26-Jul-13 8:12 
QuestionError Handling? Pin
fwp4026-Jun-13 8:01
memberfwp4026-Jun-13 8:01 
Questiongood job! Pin
ftai20-Jun-13 23:07
memberftai20-Jun-13 23:07 
GeneralMy vote of 5 Pin
ftai20-Jun-13 23:06
memberftai20-Jun-13 23:06 
QuestionCompile errors vc++ 2010 Pin
ruy ruiz13-Jun-13 8:42
memberruy ruiz13-Jun-13 8:42 
Questionexample? Pin
ganfeng20034-Jun-13 17:02
memberganfeng20034-Jun-13 17:02 
Questionconsole example? Pin
ganfeng20034-Jun-13 17:00
memberganfeng20034-Jun-13 17:00 
AnswerRe: console example? Pin
otom4-Jun-13 18:53
memberotom4-Jun-13 18:53 
QuestionRe: console example? Pin
lion_11710-Aug-14 21:14
memberlion_11710-Aug-14 21:14 
GeneralMy vote of 5 Pin
ganfeng20034-Jun-13 16:56
memberganfeng20034-Jun-13 16:56 
Suggestionone more adjustment for Linux Pin
Member 860761216-Apr-13 9:57
memberMember 860761216-Apr-13 9:57 
GeneralWorks like a charm Pin
rob_toutant7-Mar-13 21:54
memberrob_toutant7-Mar-13 21:54 
Questionsome small adjustments for Linux Pin
grouby7521-Feb-13 2:19
membergrouby7521-Feb-13 2:19 
Questionhow to upload large File (>4GB) Pin
gcpony26-Dec-12 22:48
membergcpony26-Dec-12 22:48 
GeneralMy vote of 5 Pin
Mihai MOGA14-Dec-12 6:11
memberMihai MOGA14-Dec-12 6:11 
GeneralMy vote of 5 Pin
justdownloads13-Dec-12 0:46
memberjustdownloads13-Dec-12 0:46 
Questionis there resource leak? Pin
haifeng21714-Aug-12 4:33
memberhaifeng21714-Aug-12 4:33 
AnswerRe: is there resource leak? Pin
warmice525513-May-14 21:35
memberwarmice525513-May-14 21:35 
Questiondownload files with filename matching Pin
mallouna4-Jul-12 22:28
membermallouna4-Jul-12 22:28 
SuggestionGreat Library small bug for File Modification Time Pin
Lefterisg30-May-12 6:03
memberLefterisg30-May-12 6:03 
QuestionChange protocol Type Pin
jazaman14-May-12 19:31
memberjazaman14-May-12 19:31 
QuestionCan't compile the code Pin
Hooch18012-Feb-12 4:35
memberHooch18012-Feb-12 4:35 
AnswerRe: Can't compile the code Pin
Frank Heimes11-Mar-12 23:09
memberFrank Heimes11-Mar-12 23:09 
QuestionCalling of ::List() does not react at all Pin
Member 440367423-Dec-11 5:00
memberMember 440367423-Dec-11 5:00 
AnswerRe: Calling of ::List() does not react at all Pin
otom23-Dec-11 6:00
memberotom23-Dec-11 6:00 
GeneralRe: Calling of ::List() does not react at all Pin
Member 440367423-Dec-11 6:54
memberMember 440367423-Dec-11 6:54 
GeneralRe: Calling of ::List() does not react at all Pin
Member 863475024-Apr-13 1:19
memberMember 863475024-Apr-13 1:19 
BugSending/Receiving data using select() Pin
KHSIEMENS21-Dec-11 21:56
memberKHSIEMENS21-Dec-11 21:56 
QuestionFtp Onresponse event Pin
pippo pioppo20-Dec-11 0:00
memberpippo pioppo20-Dec-11 0:00 
QuestionLicence?? Pin
pippo pioppo8-Dec-11 23:45
memberpippo pioppo8-Dec-11 23:45 
AnswerRe: Licence?? Pin
otom11-Dec-11 6:07
memberotom11-Dec-11 6:07 
QuestionGetting Pin
Saviovt1020-Nov-11 23:12
memberSaviovt1020-Nov-11 23:12 
AnswerRe: Getting Pin
otom11-Dec-11 6:13
memberotom11-Dec-11 6:13 

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.

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