Description

This article presents a fully functional implementation of a FTP server. It can handle multiple connections at the same time (multi threaded) and has most of the features you would find in other commercial/shareware FTP servers. The server handles all basic FTP commands and offers easy user account management.

This article describes the most important classes of the application:

CFTPServer

This class is in fact the FTP server, and controls all other classes needed for the server to work. Although CFTPServer is part of a dialog based application, it does not rely on a User Interface and can easily be implemented in a service or console application as well.

Deactivates the server and disconnects all connected clients by terminating the running threads.

BOOL IsActive()

Is FTP server active?

void SetMaxUsers(int nValue)

Set maximum number of users.

void SetPort(int nValue)

Set listening port for new connections.

void SetTimeout(int nValue)

Set connection timeout (in ms). When a client does not send any commands for nValue ms, the server will close the connection.

void SetWelcomeMessage(LPCTSTR lpszText)

Set the text that will be displayed when the client logs on.

void Initialize(CFTPEventSink *pEventSink)

Set the event sink. The event sink will be the window (or any class) that receives the events generated by the FTP server. See CFTPEventSink description for more info.

CFTPEventSink

To be able to 'send' events from the CFTPServer class to the main application, I used multiple inheritance and virtual functions. The CFTPEventSink is just a helper class that contains nothing else than virtual functions, when you derive your class from CFTPEventSink these virtual functions become a kind of events. The class CFTPServer has a reference to this class and calls the virtual functions when it needs to notify the application.

A statistic has changed, for example the number of downloaded or uploaded files.

Other helper classes:

CUserManager

The class CUserManager handles all user and file related stuff. It checks the connected users for their access rights and converts remote to local paths. CUserManager uses serializing for storing and loading the user settings.

CListenSocket

This socket is part of CFTPServer and accepts incoming connections. When a clients connects to the server, CListenSocket accepts the connection and creates a new thread (CConnectThread) that will take care of all further communication between the client and the server. After the thread has been created, CListenSocket will return to its waiting state.

CConnectThread

This thread will handle all communication between the client and the server using CConnectSocket.

CConnectSocket

This socket class will process all incoming FTP commands and send back the response to the client.

CDataSocket

When data needs to be send or received, a CDataSocket will be created by CConnectSocket. The CDataSocket class will transfer this data (such as directory listings and files) on a separate port.

All the other classes are just UI related and are only included to make it look a little bit fancier.

CFTPServer Usage:

To use the class in your application, you need to do the following:

Add the class to your application.

Derive your main class from CFTPEventSink.

Override the virtual functions of CFTPEventSink; these are the events that come from the server.

Contacting the Author

Credits

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.

I have a small doubt in pasv command. My local system is connected to net using a broadband modem where it is connected via a router. If i want to connect to my ftp server from out side over net i need to use my external ip and its working fine getting connected, but it works only till executing the pasv command and no file list is displayed, my doubt is when responding for the pasv command i can send only my internal ip and a new connection to view the file list is not getting connected.

Below is the sample code. Actually this is working fine few months back, recently i had changed most of the parts and i dont know what happend to this.

Obtaining an external ip and sending it in in pasv response may work fine, but it was not an actual solution as the same code was working before few months back. Can you please help me out to solve this porblem. I am working on my college project with this and i need it as soon as possible. Thank you...

I have a question about stor command.
When control socket recive a "stor" command,control socket calls CreateDataConnection to connect to client and then prepare to recive the file.
If OnConnect be called just after connect. And client sends file at once, control socket have no time to
call RetrieveFile and PrepareReceiveFile won't be called. So File cannot be recviced correctly.

It would be the IP address of the computer which you have installed FTP Server on. Also if you have changed the port number you will need to modify the address to include the port number if its not 21.

I have recently been looking into making my own FTP server. So I downloaded from CodeProject your FTP Server written in C++/MFC. It works really well, and it was very useful, however I thought I would point out that I believe that there is a bug in that version. If you push too many files, or even just a single large file it will stop uploading at 1064960 bytes, and (most clients, I use Filezilla... as I am an opensource fan) will ask you if you would like to overnight the file even if you haven't finished uploading it. I have been looking at your code, and I cannot see the reason for this

I thought I would bring this to your attention. You might have already fixed this error in later versions which I noticed you have created from your website, e.g the Baby FTP Server and the QuickNEasy one. May I know how to fix this bug?

neither this FTP server, nor Baby FTP are not supports files greater than 4Gb
i've fixed it by simple using I64 modificator in format string in CControlSocket::GetDirectoryList [ strLength.Format("%I64d", find.GetLength()); ] and using of ULONGLONG as type for CDataSocket::m_nTotal* variables

Hi !,
I found this article is interesting and wanted to use the source in my project.
I have to develop a FTP server with minimum functionality (uploading and downloding the files).
Please let me know if I use the source is there any licensing problems ?
An early reply appriciated.