Click here to Skip to main content
6,293,171 members and growing! (11,972 online)
Email Password   helpLost your password?
General Programming » Internet / Network » FTP     Intermediate

A Complete FTP Server

By Pablo van der Meer

This article presents a fully functional implementation of a FTP server.
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:11 Jul 2002
Updated:30 May 2005
Views:220,438
Bookmarked:96 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
56 votes for this article.
Popularity: 7.68 Rating: 4.39 out of 5
1 vote, 2.8%
1

2

3
2 votes, 5.6%
4
33 votes, 91.7%
5

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


Member
Pablo has been programming in C/C++ for 8 years and Visual C++/MFC for 6 years.
He is interested in music, blonde women and programming. His background includes telecommunication, electronics and software engineering, and he is currently based in 't Westland, The Netherlands.
Pablo van der Meer is developing software for a small Dutch telecom company. Besides programming Pablo is active as a dj/producer and creates various styles of music.

Check out my website for lots of other MFC articles:http://www.pablosoftwaresolutions.com

Occupation: Web Developer
Location: Netherlands Netherlands

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 112 (Total in Forum: 112) (Refresh)FirstPrevNext
GeneralTrying to understand what the dummy window for message routing is trying to do Pinmembertom234997:55 28 Feb '09  
GeneralA question about STOR command PinmemberJason( J.Zhang)22:13 10 Dec '08  
Generalhow to find ftp address for my server PinmemberNprabahar8:39 23 Sep '08  
GeneralRe: how to find ftp address for my server Pinmembertom2349918:00 8 Mar '09  
GeneralConverting application to object Pinmembermcunha986:01 2 Jul '08  
QuestionBug... 1064960 bytes and then it blocks.... PinmemberG_T7:02 29 May '08  
GeneralC++ .NET Source Pinmemberjmanson7:16 2 May '08  
GeneralHow can I get c# source code? PinmemberLeSang16:42 23 Aug '07  
GeneralRe: How can I get c# source code? PinmemberAghochikyan3:19 9 Sep '07  
GeneralGot errors while compling the FTPSERVER in 2.0 framework Pinmembermesh21238817:15 29 Jul '07  
Generalcan not run in true 64 bit Pinmember6:56 22 Feb '07  
QuestionFTP url tracing Pinmemberamitbk81@yahoo.com21:22 24 Jan '07  
GeneralFTP url tracing Pinmemberamitbk81@yahoo.com21:21 24 Jan '07  
General64-bit file size Pinmemberstd.denis3:15 7 Jan '07  
Generalhow to run with chinese file and folder name Pinmemberfnjcr20:40 7 Dec '06  
Generalmultithreaded download Pinmemberlathi2:36 26 May '06  
QuestionA Complete FTP Server source usage PinmemberViswanatha Shastry21:05 27 Apr '06  
GeneralMemory leak problem. PinmemberVCPP616:20 31 Oct '04  
GeneralMessage Title: Socket Notification Sink: FtpServer.exe PinsussAnonymous15:48 2 Nov '04  
GeneralRe: Message Title: Socket Notification Sink: FtpServer.exe PinmemberPablo van der Meer21:18 2 Nov '04  
GeneralBug report in DataSocket.cpp PinmemberVCPP69:42 9 Sep '04  
GeneralRe: Bug report in DataSocket.cpp PinmemberPablo van der Meer10:41 9 Sep '04  
GeneralRe: Bug report in DataSocket.cpp PinmemberVCPP615:45 9 Sep '04  
GeneralBug report in OnReceive function PinsussJim Fougeron11:56 26 Jun '04  
GeneralCompiling in VS.NET 2003 PinmemberShnaider16:04 29 May '04  

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

PermaLink | Privacy | Terms of Use
Last Updated: 30 May 2005
Editor: Smitha Vijayan
Copyright 2002 by Pablo van der Meer
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project