Click here to Skip to main content
13,803,561 members
Click here to Skip to main content
Add your own
alternative version


165 bookmarked
Posted 11 Jul 2002

A Complete FTP Server

, 30 May 2005
Rate this:
Please Sign up or sign in to vote.
This article presents a fully functional implementation of a FTP server.

Sample Image


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:


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.

  • BOOL Start()

    Activates the FTP server. It opens a listening socket (port 21) to accept connections.

  • void Stop()

    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.


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.

The following 'events' are available:

  • void OnFTPUserConnected(DWORD nThreadID, LPCTSTR lpszUser, LPCSTR lpszAddress);

    A client has successfully connected.

  • void OnFTPUserDisconnected(DWORD nThreadID, LPCTSTR lpszUser);

    A client has disconnected.

  • void OnFTPStatusChange(int nType, LPCTSTR lpszText);

    FTP server status has changed (file downloaded/uploaded).

  • void OnFTPReceivedBytesChange(int nBytes);

    The number of received bytes has changed.

  • void OnFTPSentBytesChange(int nBytes);

    The number of sent bytes received has changed.

  • void OnFTPStatisticChange(int nType, int nValue);

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

Other helper classes:


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.


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.


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


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


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:

  1. Add the class to your application.
  2. Derive your main class from CFTPEventSink.
  3. Override the virtual functions of CFTPEventSink; these are the events that come from the server.
  4. Initialize the eventsink.
  5. Start the server.
class CMyDlg : public CDialog, CFTPEventSink

    virtual void OnFTPUserConnected(DWORD nThreadID, 
                 LPCTSTR lpszUser, LPCSTR lpszAddress);
    virtual void OnFTPUserDisconnected(DWORD nThreadID, LPCTSTR lpszUser);
    virtual void OnFTPStatusChange(int nType, LPCTSTR lpszText);
    virtual void OnFTPReceivedBytesChange(int nBytes);
    virtual void OnFTPSentBytesChange(int nBytes);
    virtual void OnFTPStatisticChange(int nType, int nValue);


BOOL CMyDlg::OnInitDialog()

    // initialize event sink
    // set maximum users to 10
    // accept new connections on port 21
    // activate server

    return TRUE;

Contacting the Author

For any updates to this article, check my site.


Inspired by FileZilla Server.


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

Pablo van der Meer
Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

Questionmemery leak&“Encountered an improper argument” Pin
Member 1258877820-Jun-16 0:20
memberMember 1258877820-Jun-16 0:20 
Question* Pin
Алексей115324-Jun-14 1:22
memberАлексей115324-Jun-14 1:22 
GeneralAsking a Guarantee of Permission For Us From Developer Pin
Violet_Lotus21-Apr-12 8:36
memberViolet_Lotus21-Apr-12 8:36 
GeneralMy vote of 5 Pin
darvenLee25-Aug-10 22:57
memberdarvenLee25-Aug-10 22:57 
GeneralHave a doubt in pasv command Pin
Sabari Girishwar Rao10-Oct-09 8:07
memberSabari Girishwar Rao10-Oct-09 8:07 
GeneralTrying to understand what the dummy window for message routing is trying to do Pin
Victor679928-Feb-09 7:55
memberVictor679928-Feb-09 7:55 
GeneralA question about STOR command Pin
Jason( J.Zhang)10-Dec-08 22:13
memberJason( J.Zhang)10-Dec-08 22:13 
Questionhow to find ftp address for my server Pin
Nprabahar23-Sep-08 8:39
memberNprabahar23-Sep-08 8:39 
AnswerRe: how to find ftp address for my server Pin
Victor67998-Mar-09 18:00
memberVictor67998-Mar-09 18:00 
GeneralConverting application to object Pin
mcunha982-Jul-08 6:01
membermcunha982-Jul-08 6:01 
QuestionBug... 1064960 bytes and then it blocks.... Pin
G_T29-May-08 7:02
memberG_T29-May-08 7:02 
GeneralC++ .NET Source Pin
jmanson2-May-08 7:16
memberjmanson2-May-08 7:16 
QuestionHow can I get c# source code? Pin
synbad23-Aug-07 16:42
membersynbad23-Aug-07 16:42 
AnswerRe: How can I get c# source code? Pin
Aghochikyan9-Sep-07 3:19
memberAghochikyan9-Sep-07 3:19 
GeneralGot errors while compling the FTPSERVER in 2.0 framework Pin
mesh21238829-Jul-07 17:15
membermesh21238829-Jul-07 17:15 
GeneralRe: Got errors while compling the FTPSERVER in 2.0 framework Pin
Robert nAppliance31-Aug-11 15:39
memberRobert nAppliance31-Aug-11 15:39 
Questioncan not run in true 64 bit Pin
swliu_sysu22-Feb-07 6:56
memberswliu_sysu22-Feb-07 6:56 
QuestionFTP url tracing Pin
amitbk81@yahoo.com24-Jan-07 21:22
memberamitbk81@yahoo.com24-Jan-07 21:22 
GeneralFTP url tracing Pin
amitbk81@yahoo.com24-Jan-07 21:21
memberamitbk81@yahoo.com24-Jan-07 21:21 
General64-bit file size Pin
std.denis7-Jan-07 3:15
memberstd.denis7-Jan-07 3:15 
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
Questionhow to run with chinese file and folder name Pin
fnjcr7-Dec-06 20:40
memberfnjcr7-Dec-06 20:40 
Generalmultithreaded download Pin
lathi26-May-06 2:36
memberlathi26-May-06 2:36 
QuestionA Complete FTP Server source usage Pin
Viswanatha Shastry27-Apr-06 21:05
memberViswanatha Shastry27-Apr-06 21:05 
GeneralMemory leak problem. Pin
VCPP631-Oct-04 16:20
memberVCPP631-Oct-04 16:20 
GeneralMessage Title: Socket Notification Sink: FtpServer.exe Pin
Anonymous2-Nov-04 15:48
memberAnonymous2-Nov-04 15:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Cookies | Terms of Use | Mobile
Web06 | 2.8.181215.1 | Last Updated 30 May 2005
Article Copyright 2002 by Pablo van der Meer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid