5,557,174 members and growing! (16,260 online)
Email Password   helpLost your password?
General Programming » Internet / Network » FTP     Intermediate

FTP Client Class

By otom

A non-MFC class to encapsulate the FTP protocol.
VC7, VC7.1, VC8.0, C++Windows, Win2K, WinXP, MFC, STL, VS.NET2003, Visual Studio, Dev

Posted: 27 Oct 2004
Updated: 5 Dec 2005
Views: 99,717
Bookmarked: 76 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
40 votes for this Article.
Popularity: 7.58 Rating: 4.73 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 2.6%
3
3 votes, 7.7%
4
35 votes, 89.7%
5

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

About the Author

otom



Occupation: Software Developer (Senior)
Location: Germany Germany

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 86 (Total in Forum: 86) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralA very easy port to LinuxmemberMember 45551727:14 30 Sep '08  
GeneralA comfortable FTP class in .NETmemberElmue13:18 27 Aug '08  
GeneralHas anyone got this to compile with VC6?memberpscholl6:28 21 Jul '08  
General...memberAHTOXA3:41 18 Jun '08  
GeneralSOCKET handle(Control Link which listen to accept data link) unclosed when calling UploadmemberMember 458698623:10 13 Apr '08  
GeneralRe: SOCKET handle(Control Link which listen to accept data link) unclosed when calling Uploadmemberotom8:50 14 Apr '08  
Questionwhy the port is not released when I use port mode?member98863019:07 18 Dec '07  
GeneralRe: why the port is not released when I use port mode?memberotom20:22 27 Jan '08  
GeneralMFC compatabilitymembertriplebit5:18 9 Nov '07  
GeneralReply error after upload/download filesmembercalvfoo1:20 1 Nov '07  
QuestionA program about UpLoadFilememberlihe87654403120:12 7 Oct '07  
AnswerRe: A program about UpLoadFilememberotom8:39 8 Oct '07  
GeneralRe: A program about UpLoadFilememberlihe87654403117:09 8 Oct '07  
GeneralRe: A program about UpLoadFilememberotom20:37 16 Oct '07  
GeneralRe: A program about UpLoadFilememberlihe87654403122:32 17 Oct '07  
GeneralRe: A program about UpLoadFilememberotom10:14 18 Oct '07  
GeneralRe: A program about UpLoadFilememberlihe87654403121:19 22 Oct '07  
Questionlinkage errorsmembergabriellopes15:35 19 Jun '07  
AnswerRe: linkage errorsmemberLushan11120:40 19 Jun '07  
GeneralRe: linkage errorsmembergmlopes4:08 20 Jun '07  
GeneralRe: linkage errorsmembergmlopes7:52 20 Jun '07  
GeneralBug on connection break?memberLushan1113:17 14 Jun '07  
GeneralRe: Bug on connection break?memberotom10:25 14 Jun '07  
GeneralRe: Bug on connection break? [modified]memberLushan11117:11 14 Jun '07  
GeneralRe: Bug on connection break? [modified]memberLushan11121:46 14 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Dec 2005
Editor: Rinish Biju
Copyright 2004 by otom
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project