Click here to Skip to main content
Licence CPOL
First Posted 27 Oct 2004
Views 210,807
Downloads 13,423
Bookmarked 137 times

FTP Client Class

By otom | 5 Dec 2005
A non-MFC class to encapsulate the FTP protocol.

1

2
1 vote, 2.1%
3
3 votes, 6.3%
4
44 votes, 91.7%
5
4.82/5 - 49 votes
1 removed
μ 4.77, σa 0.87 [?]

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, 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

Member


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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCalling of ::List() does not react at all PinmemberMember 44036746:00 23 Dec '11  
AnswerRe: Calling of ::List() does not react at all Pinmemberotom7:00 23 Dec '11  
GeneralRe: Calling of ::List() does not react at all [modified] PinmemberMember 44036747:54 23 Dec '11  
BugSending/Receiving data using select() PinmemberKHSIEMENS22:56 21 Dec '11  
QuestionFtp Onresponse event [modified] Pinmemberpippo pioppo1:00 20 Dec '11  
QuestionLicence?? Pinmemberpippo pioppo0:45 9 Dec '11  
AnswerRe: Licence?? Pinmemberotom7:07 11 Dec '11  
QuestionGetting PinmemberSaviovt100:12 21 Nov '11  
AnswerRe: Getting Pinmemberotom7:13 11 Dec '11  
QuestionReg: Writing a stream over FTPSession PinmemberSivaram Kannan5:10 3 Aug '11  
QuestionRegarding secure FTP PinmemberSaviovt1020:51 12 Jul '11  
AnswerRe: Regarding secure FTP Pinmemberotom22:03 12 Jul '11  
GeneralWSAStartup Problem Pinmemberwolf305715:58 2 Jan '11  
Generalerror when download file to strLocalFile which contains chinese word PinmemberSeshaal18:36 4 May '10  
GeneralAbort takes a lot of time! PinmemberSpringMVC1:43 19 Mar '10  
QuestionAny one can help me to download file on ftp server recursively by using these codes? Pinmembersongge22:28 17 Dec '09  
GeneralCompilling and running under VC++ 2002 PinmemberLeandro T C Melo5:28 14 May '09  
GeneralRe: Compilling and running under VC++ 2002 PinmemberJackSimmons14:17 1 Jun '09  
GeneralBug in CFTPClient::UploadFile PinmemberGluck0:12 24 Apr '09  
General[Message Deleted] Pinmemberit.ragester22:58 2 Apr '09  
GeneralA very easy port to Linux PinmemberMember 45551727:14 30 Sep '08  
GeneralA comfortable FTP class in .NET PinmemberElmue13:18 27 Aug '08  
QuestionHas anyone got this to compile with VC6? Pinmemberpscholl6:28 21 Jul '08  
AnswerRe: Has anyone got this to compile with VC6? PinmemberDamDaDum4:26 28 Oct '08  
General... PinmemberAHTOXA3:41 18 Jun '08  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120209.1 | Last Updated 6 Dec 2005
Article Copyright 2004 by otom
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid