Click here to Skip to main content
11,803,019 members (64,728 online)
Click here to Skip to main content

FTP Client Class

, 8 Dec 2012 CPOL 356.9K 33.2K 223
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

QuestionBug on connection break? Pin
Lushan11114-Jun-07 2:17
memberLushan11114-Jun-07 2:17 
AnswerRe: Bug on connection break? Pin
otom14-Jun-07 9:25
memberotom14-Jun-07 9:25 
GeneralRe: Bug on connection break? [modified] Pin
Lushan11114-Jun-07 16:11
memberLushan11114-Jun-07 16:11 
GeneralRe: Bug on connection break? [modified] Pin
Lushan11114-Jun-07 20:46
memberLushan11114-Jun-07 20:46 
GeneralRe: Bug on connection break? Pin
otom17-Jun-07 20:29
memberotom17-Jun-07 20:29 
GeneralRe: Bug on connection break? Pin
Lushan11120-Jun-07 16:02
memberLushan11120-Jun-07 16:02 
GeneralUnix port Pin
MarcoNedwig5-May-07 23:27
memberMarcoNedwig5-May-07 23:27 
GeneralWinsock library Pin
wous4-Mar-07 10:59
memberwous4-Mar-07 10:59 
Answer[TIP] Linking in DEV C++ Pin
RAD CPP16-Aug-07 11:19
memberRAD CPP16-Aug-07 11:19 
GeneralMinor bug - GetPeerAddr declaration missing in IBlockingSocket Pin
toesinsock17-Oct-06 19:14
membertoesinsock17-Oct-06 19:14 
Questionhow can it work in the No.21 port? Pin
bobcow14-Oct-06 1:45
memberbobcow14-Oct-06 1:45 
AnswerRe: how can it work in the No.21 port? Pin
Lushan11119-Jun-07 19:37
memberLushan11119-Jun-07 19:37 
GeneralCompile Problems Pin
Dark_Guru4-Sep-06 21:36
memberDark_Guru4-Sep-06 21:36 
GeneralProblems Including your class (im a noob =[ ) Pin
RedSpyder29-Jun-06 11:05
memberRedSpyder29-Jun-06 11:05 
hi! My name is JC im from Puerto Rico. Right now my C++ knowledge is a bit "limited"
and Im developing an application tthat will end up updating a txt file in an FTP. Your class got the perfect functions i need. Exactly what i need for my progrm to work is this:

nsFTP::CFTPClient ftpClient;
nsFTP::CLogonInfo logonInfo("localhost", 21, "anonymous",

// connect to server

// do file operations
ftpClient.DownloadFile("/pub/test.txt", "c:\\temp\\test.txt");

ftpClient.UploadFile("c:\\temp\\test.txt", "/upload/test.txt");

// disconnect

dat is basically all i need BUT your work is very hard to understand for me and i just dont know WHAT to include and where to include.. Id appreciate any help you can give me. Thanks in advance

Generalftp download(multithreaded) Pin
lathi28-May-06 2:28
memberlathi28-May-06 2:28 
QuestionHow to modify this class to support IPv6 Pin
coolfresco15-May-06 2:24
membercoolfresco15-May-06 2:24 
GeneralDuring transfer bug / question Pin
unit15812-Mar-06 19:24
memberunit15812-Mar-06 19:24 
GeneralRe: During transfer bug / question Pin
otom7-Apr-06 23:01
memberotom7-Apr-06 23:01 
GeneralCompiler Errors using MSDEV2003 Pin
Stephan Pilz18-Jan-06 1:48
memberStephan Pilz18-Jan-06 1:48 
GeneralGetting a lot of redefinition compiler errors when compiling console project Pin
mr.mvp11-Jan-06 5:04
membermr.mvp11-Jan-06 5:04 
QuestionHow can I implement the resuming using this codes? Pin
onlysmooth14-Dec-05 1:33
memberonlysmooth14-Dec-05 1:33 
AnswerRe: How can I implement the resuming using this codes? Pin
otom14-Dec-05 3:31
memberotom14-Dec-05 3:31 
GeneralCongratulations with new version ! Pin
kaponka8-Dec-05 3:45
memberkaponka8-Dec-05 3:45 
QuestionHow can I upload files? Pin
ThomasWong20001-Dec-05 22:59
memberThomasWong20001-Dec-05 22:59 
GeneralMemory Leaks Pin
pdrummond7-Nov-05 5:51
memberpdrummond7-Nov-05 5:51 
GeneralRe: Memory Leaks Pin
OTOM26-Nov-05 3:28
memberOTOM26-Nov-05 3:28 
GeneralVisual C++ 6.0 Pin
MartinSo4-Apr-06 22:29
memberMartinSo4-Apr-06 22:29 
GeneralRe: Visual C++ 6.0 Pin
machengqi20-Aug-07 21:01
membermachengqi20-Aug-07 21:01 
AnswerRe: Memory Leaks (Solved!) Pin
Member 157947512-Nov-09 20:09
memberMember 157947512-Nov-09 20:09 
QuestionCan you add multithread download(upload) function to next release edtion? Pin
mrduanjh5-Nov-05 0:45
membermrduanjh5-Nov-05 0:45 
GeneralGet the progress percent without the progress dialog Pin
FlyingDancer1-Nov-05 14:57
memberFlyingDancer1-Nov-05 14:57 
GeneralRe: Get the progress percent without the progress dialog Pin
FlyingDancer2-Nov-05 2:55
memberFlyingDancer2-Nov-05 2:55 
GeneralRe: Get the progress percent without the progress dialog Pin
FlyingDancer8-Nov-05 22:43
memberFlyingDancer8-Nov-05 22:43 
GeneralRe: Get the progress percent without the progress dialog Pin
FlyingDancer8-Nov-05 1:50
memberFlyingDancer8-Nov-05 1:50 
GeneralRe: Get the progress percent without the progress dialog Pin
onlysmooth11-Dec-05 2:12
memberonlysmooth11-Dec-05 2:12 
GeneralRe: Get the progress percent without the progress dialog Pin
FlyingDancer12-Dec-05 19:23
memberFlyingDancer12-Dec-05 19:23 
GeneralRe: Get the progress percent without the progress dialog Pin
onlysmooth14-Dec-05 1:25
memberonlysmooth14-Dec-05 1:25 
Questionwhy download/upload become more and more slow? Pin
mrduanjh30-Oct-05 22:47
membermrduanjh30-Oct-05 22:47 
AnswerRe: why download/upload become more and more slow? Pin
bobcow25-Sep-06 17:16
memberbobcow25-Sep-06 17:16 
QuestionConsole Pin
PENCIL213-Oct-05 19:43
memberPENCIL213-Oct-05 19:43 
AnswerRe: Console Pin
OTOM14-Oct-05 4:09
memberOTOM14-Oct-05 4:09 
GeneralRe: Console Pin
Merijn27-Nov-05 21:47
memberMerijn27-Nov-05 21:47 
GeneralRe: Console Pin
OTOM28-Nov-05 8:09
memberOTOM28-Nov-05 8:09 
GeneralRe: Console Pin
Merijn28-Nov-05 20:04
memberMerijn28-Nov-05 20:04 
GeneralTransfer Mode Problem Pin
Ralf Kunsmann21-Sep-05 20:44
memberRalf Kunsmann21-Sep-05 20:44 
Questionmsvcr71d.dll Exception Pin
FlyingDancer13-Sep-05 23:56
memberFlyingDancer13-Sep-05 23:56 
AnswerRe: msvcr71d.dll Exception Pin
FlyingDancer14-Sep-05 19:23
memberFlyingDancer14-Sep-05 19:23 
Questiontrying to compile using dev-c++ Pin
georgeblunt28-Aug-05 21:51
membergeorgeblunt28-Aug-05 21:51 
AnswerRe: trying to compile using dev-c++ Pin
OTOM14-Sep-05 7:49
memberOTOM14-Sep-05 7:49 
GeneralRe: trying to compile using dev-c++ Pin
georgeblunt15-Sep-05 9:17
membergeorgeblunt15-Sep-05 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.151002.1 | Last Updated 9 Dec 2012
Article Copyright 2004 by otom
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid