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
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.
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:
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,
A simple data structure for logon information, such as host, username, password, firewall, ...
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::CLogonInfo logonInfo("localhost", 21, "anonymous",
for( nsFTP::TSpFTPFileStatusVector::iterator it=list.begin();
it!=list.end(); ++it )
- 2004-10-25 - First public release.
- 2005-12-04 - Version 1.1
- Some interfaces changed (e.g.
- Bug in
SendCommand was called before data connection was established.
- Bugs in
- Infinite loop if the response line doesn't end with CRLF.
- Return value of
std:string->find must be checked against
- 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.