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

FTP Client Class

, 5 Dec 2005
Rate this:
Please Sign up or sign in to vote.
A non-MFC class to encapsulate the FTP protocol.
This is an old version of the currently published article.

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 (ports to UNIX a lot easier),
  • not using other MFC-stuffs like CString (uses STL),
  • supports Firewalls,
  • supports resuming,
  • 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::CNotifaction

    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):

    nsFTP::CFTPClient ftpClient;
    nsFTP::CLogonInfo logonInfo("localhost", 21, "anonymous", 
                                          "anonymous@user.com");
    
    // connect to server
    ftpClient.Login(logonInfo);
    
    // get directory listing
    nsFTP::TSpFTPFileStatusVector list;
    ftpClient.List("/", list);
    
    // iterate listing
    for( nsFTP::TSpFTPFileStatusVector::iterator it=list.begin(); 
                                             it!=list.end(); ++it )
        TRACE("\n%s", (*it)->Name().c_str());
    
    // do file operations
    ftpClient.DownloadFile("/pub/test.txt", "c:\\temp\\test.txt");
    
    ftpClient.UploadFile("c:\\temp\\test.txt", "/upload/test.txt");
    
    ftpClient.RenameFile("/upload/test.txt", "/upload/NewName.txt");
    
    ftpClient.Delete("/upload/NewName.txt");
    
    // disconnect
    ftpClient.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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

otom
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionFile modification time Pinmembermrbogus2731-Oct-14 2:38 
Questionupload zip file bugs Pinmemberlion_11722-Aug-14 18:24 
QuestionAbort Pinmembermember 626847327-Mar-14 13:06 
QuestionProblem to upload a file by FTP PinmemberMember 1067597618-Mar-14 2:14 
AnswerRe: Problem to upload a file by FTP PinmemberAshish Tyagi 4026-Mar-14 0:10 
GeneralRe: Problem to upload a file by FTP PinmemberMember 1067597626-Mar-14 0:15 
GeneralRe: Problem to upload a file by FTP PinmemberMember 1067597626-Mar-14 2:19 
GeneralRe: Problem to upload a file by FTP PinmemberAshish Tyagi 4026-Mar-14 7:01 
QuestionIssue trying to build in QTCreator on Ubuntu 12 PinmemberNeriam18-Feb-14 7:18 
AnswerRe: Issue trying to build in QTCreator on Ubuntu 12 Pinmemberomid1205-Nov-14 22:35 
QuestionDoes it append to a file? PinmemberKiranV278-Aug-13 6:43 
QuestionProblem with connect PinmemberMartin Perry26-Jul-13 9:12 
QuestionError Handling? Pinmemberfwp4026-Jun-13 9:01 
Questiongood job! Pinmemberftai21-Jun-13 0:07 
GeneralMy vote of 5 Pinmemberftai21-Jun-13 0:06 
QuestionCompile errors vc++ 2010 Pinmemberruy ruiz13-Jun-13 9:42 
Questionexample? Pinmemberganfeng20034-Jun-13 18:02 
Questionconsole example? Pinmemberganfeng20034-Jun-13 18:00 
AnswerRe: console example? Pinmemberotom4-Jun-13 19:53 
QuestionRe: console example? Pinmemberlion_11710-Aug-14 22:14 
GeneralMy vote of 5 Pinmemberganfeng20034-Jun-13 17:56 
Suggestionone more adjustment for Linux PinmemberMember 860761216-Apr-13 10:57 
GeneralWorks like a charm Pinmemberrob_toutant7-Mar-13 22:54 
Questionsome small adjustments for Linux Pinmembergrouby7521-Feb-13 3:19 
Questionhow to upload large File (>4GB) Pinmembergcpony26-Dec-12 23:48 
GeneralMy vote of 5 PinmemberMihai MOGA14-Dec-12 7:11 
GeneralMy vote of 5 Pinmemberjustdownloads13-Dec-12 1:46 
Questionis there resource leak? Pinmemberhaifeng21714-Aug-12 5:33 
AnswerRe: is there resource leak? Pinmemberwarmice525513-May-14 22:35 
Questiondownload files with filename matching Pinmembermallouna4-Jul-12 23:28 
SuggestionGreat Library small bug for File Modification Time PinmemberLefterisg30-May-12 7:03 
QuestionChange protocol Type Pinmemberjazaman14-May-12 20:31 
QuestionCan't compile the code PinmemberHooch18012-Feb-12 5:35 
AnswerRe: Can't compile the code PinmemberFrank Heimes12-Mar-12 0:09 
QuestionCalling of ::List() does not react at all PinmemberMember 440367423-Dec-11 6:00 
AnswerRe: Calling of ::List() does not react at all Pinmemberotom23-Dec-11 7:00 
GeneralRe: Calling of ::List() does not react at all [modified] PinmemberMember 440367423-Dec-11 7:54 
GeneralRe: Calling of ::List() does not react at all PinmemberMember 863475024-Apr-13 2:19 
BugSending/Receiving data using select() PinmemberKHSIEMENS21-Dec-11 22:56 
QuestionFtp Onresponse event [modified] Pinmemberpippo pioppo20-Dec-11 1:00 
QuestionLicence?? Pinmemberpippo pioppo9-Dec-11 0:45 
AnswerRe: Licence?? Pinmemberotom11-Dec-11 7:07 
QuestionGetting PinmemberSaviovt1021-Nov-11 0:12 
AnswerRe: Getting Pinmemberotom11-Dec-11 7:13 
QuestionReg: Writing a stream over FTPSession PinmemberSivaram Kannan3-Aug-11 5:10 
QuestionRegarding secure FTP PinmemberSaviovt1012-Jul-11 20:51 
AnswerRe: Regarding secure FTP Pinmemberotom12-Jul-11 22:03 
GeneralWSAStartup Problem Pinmemberwolf30572-Jan-11 15:58 
Generalerror when download file to strLocalFile which contains chinese word PinmemberSeshaal4-May-10 18:36 
GeneralAbort takes a lot of time! PinmemberSpringMVC19-Mar-10 1:43 

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
Web01 | 2.8.1411023.1 | Last Updated 6 Dec 2005
Article Copyright 2004 by otom
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid