Click here to Skip to main content
Click here to Skip to main content

A Complete FTP Server

By , 30 May 2005
 

Sample Image

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.

  • 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.

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.

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:

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:

  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
{
    ...

    CFTPServer m_FTPSERVER;
    
    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()
{
    CDialog::OnInitDialog();
    
    ...

    // initialize event sink
    m_FTPSERVER.Initialize(this);
    // set maximum users to 10
    m_FTPSERVER.SetMaxUsers(10);
    // accept new connections on port 21
    m_FTPSERVER.SetPort(21);
    // activate server
    m_FTPSERVER.Start();

    return TRUE;
}

Contacting the Author

For any updates to this article, check my site.

Credits

Inspired by FileZilla Server.

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

Pablo van der Meer
Web Developer
Netherlands Netherlands
Member
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General64-bit file sizememberstd.denis7 Jan '07 - 2:15 
Questionhow to run with chinese file and folder namememberfnjcr7 Dec '06 - 19:40 
Generalmultithreaded downloadmemberlathi26 May '06 - 1:36 
can anyone give me the approach to download a file from a ftp server using multithreding. (i.e. separate threads for downloading simultaneously USING JAVA)
 
VIVEK
QuestionA Complete FTP Server source usagememberViswanatha Shastry27 Apr '06 - 20:05 
GeneralMemory leak problem.memberVCPP631 Oct '04 - 15:20 
GeneralMessage Title: Socket Notification Sink: FtpServer.exesussAnonymous2 Nov '04 - 14:48 
GeneralRe: Message Title: Socket Notification Sink: FtpServer.exememberPablo van der Meer2 Nov '04 - 20:18 
GeneralBug report in DataSocket.cppmemberVCPP69 Sep '04 - 8:42 
GeneralRe: Bug report in DataSocket.cppmemberPablo van der Meer9 Sep '04 - 9:41 
GeneralRe: Bug report in DataSocket.cppmemberVCPP69 Sep '04 - 14:45 
GeneralBug report in OnReceive functionsussJim Fougeron26 Jun '04 - 10:56 
GeneralCompiling in VS.NET 2003memberShnaider29 May '04 - 15:04 
GeneralRe: Compiling in VS.NET 2003memberShnaider29 May '04 - 15:55 
GeneralUsing scriptsmemberHellachuckles1 Feb '04 - 8:59 
GeneralRe: Using scriptsmemberPablo van der Meer1 Feb '04 - 9:39 
GeneralA word of caution....membervrosu12 Jan '04 - 10:05 
GeneralRe: A word of caution....memberPablo van der Meer12 Jan '04 - 10:22 
GeneralRe: A word of caution....membervrosu13 Jan '04 - 3:54 
QuestionHow can I capture the spying on others machine.memberHimadrish Laha26 Nov '03 - 19:44 
AnswerRe: How can I capture the spying on others machine.sussAnonymous2 Dec '03 - 3:52 
GeneralRe: How can I capture the spying on others machine.sussAnonymous2 Dec '03 - 16:57 
GeneralProblem with Using MFC in a Static librarymemberyydlh5 Nov '03 - 14:13 
GeneralRe: Problem with Using MFC in a Static librarymemberPablo van der Meer5 Nov '03 - 21:12 
GeneralRe: Problem with Using MFC in a Static librarymemberyydlh5 Nov '03 - 22:34 
QuestionBug on CInternetSession::SetOption?membernaghicalin16 Oct '03 - 11:31 
GeneralFtp Connection not workingmembergirishnadig18 Sep '03 - 6:31 
QuestionHow can I turning the server into UTF8 mode ??sussgolick11 Sep '03 - 1:04 
AnswerAny body can do it?membergolick112 Sep '03 - 4:26 
GeneralPorting CFTPServer to PocketPCmembervpitts11 Jul '03 - 13:40 
GeneralRe: Porting CFTPServer to PocketPCmemberPablo van der Meer11 Jul '03 - 21:43 
GeneralRe: Porting CFTPServer to PocketPCmembervpitts12 Jul '03 - 13:29 
GeneralRe: Porting CFTPServer to PocketPCmemberPablo van der Meer12 Jul '03 - 23:19 
GeneralRe: Porting CFTPServer to PocketPCmembervpitts13 Jul '03 - 7:08 
GeneralRe: Porting CFTPServer to PocketPCmembervpitts14 Jul '03 - 10:48 
GeneralRe: Porting CFTPServer to PocketPCmemberjimtrowii16 Dec '05 - 5:59 
Generalcrash when uploading directory recursively using WINCMD32membercoeus8 Jul '03 - 0:35 
GeneralRe: crash when uploading directory recursively using WINCMD32memberPablo van der Meer8 Jul '03 - 1:42 
GeneralCAsyncSocket::LookupHandle(...) crashing upon connectionmemberIGx8926 Jun '03 - 5:07 
GeneralRe: CAsyncSocket::LookupHandle(...) crashing upon connectionmemberPablo van der Meer26 Jun '03 - 6:39 
GeneralRe: CAsyncSocket::LookupHandle(...) crashing upon connectionmemberIGx8926 Jun '03 - 7:59 
GeneralRe: CAsyncSocket::LookupHandle(...) crashing upon connectionmemberJames Lucas29 Jun '03 - 23:34 
QuestionMultiple Data Connections???memberMubashar_Ahmad6 Jun '03 - 20:59 
AnswerRe: Multiple Data Connections???memberPablovanderMeer7 Jun '03 - 1:24 
GeneralRe: Multiple Data Connections???memberMubashar_Ahmad7 Jun '03 - 6:16 
GeneralRe: Multiple Data Connections???memberMubashar_Ahmad7 Jun '03 - 20:57 
GeneralRe: Multiple Data Connections???memberPablovanderMeer7 Jun '03 - 22:28 
Generalone problemmemberchito2 Jun '03 - 16:33 
GeneralRe: one problemmemberPablo van der Meer2 Jun '03 - 19:54 
GeneralRe: one problemmemberchito4 Jun '03 - 15:50 
Generalstatic_cast error VC++ .NetsussAnonymous16 Sep '02 - 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 30 May 2005
Article Copyright 2002 by Pablo van der Meer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid