A Complete FTP Server

简介:   Download executable - 45.4 Kb Download source - 93.8 Kb Description This article presents a fully functional implementation of a FTP server.

 

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.
Collapse Copy Code
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
目录
相关文章
|
1月前
|
网络协议 文件存储 Windows
Windows Server 2019 FTP服务器搭建
Windows Server 2019 FTP服务器搭建
|
4月前
|
网络协议 Unix 网络安全
FTP服务器怎么搭建?Windows server搭建FPT服务器
FTP服务器是按照FTP协议提供文件传输服务的计算机。它用于在两台计算机间安全地传输文件,支持用户权限管理和跨平台操作。FTP使用控制连接处理命令,数据连接传输文件,有PORT和PASV模式。要搭建FTP服务器,首先在Windows Server 2008 R2上安装IIS,确保选中FTP服务。接着,创建FTP文件夹作为站点根目录,通过IIS管理器添加FTP站点,配置站点信息、身份验证和权限。测试客户端通过telnet和浏览器访问FTP服务器,确认能成功登录及浏览文件。FTP常用于文件共享和管理,可通过专用工具如FlashFXP上传下载文件。
167 0
FTP服务器怎么搭建?Windows server搭建FPT服务器
|
存储 关系型数据库 MySQL
手把手教教会你使用Wing FTP Server安装配置并结合内网穿透实现公网访问本地站点
手把手教教会你使用Wing FTP Server安装配置并结合内网穿透实现公网访问本地站点
|
存储 负载均衡 Java
nginx+ftp搭建图片服务器(Windows Server服务器环境下)
nginx+ftp搭建图片服务器(Windows Server服务器环境下)
573 0
|
弹性计算 安全 前端开发
阿里云ECS服务器配置Web项目和FTP Server
第一次使用阿里云ECS服务器部署Web项目和FTP Server,在使用过程中遇到了很多困难,但同时对计算机网络的工作原理有了更加清晰的认识。现将使用经历进行系统性地总结。 在阅读之前请确保已购买阿里云ECS云服务器并且初始化云服务器操作系统,本教程选用的操作系统为“Windows Server 2022 数据中心版 64位中文版”。
361 0
|
Windows
阿里云Windows Server 2012 R2服务器搭建FTP服务器(被动模式)
阿里云Windows Server 2012 R2服务器搭建FTP服务器(被动模式)
727 0
阿里云Windows Server 2012 R2服务器搭建FTP服务器(被动模式)
|
网络安全 Windows
Windows 技术篇-使用Windows Server 2012 R2服务器设置ftp共享文件路径实例演示
Windows 技术篇-使用Windows Server 2012 R2服务器设置ftp共享文件路径实例演示
646 0
Windows 技术篇-使用Windows Server 2012 R2服务器设置ftp共享文件路径实例演示
|
存储 前端开发 对象存储
如何基于云存储网关SMB共享搭建FileZilla FTP server
本文介绍在windows 环境下,如何利用云存储网关提供的SMB共享,使用FileZilla Server搭建FTP服务器来访问对象存储(OSS)中的资源。
2477 0
如何基于云存储网关SMB共享搭建FileZilla FTP server
|
Windows
推荐一款ftp server工具
推荐一款ftp server工具
183 0
|
弹性计算 网络协议 网络安全
FTP Server Over 阿里云 最佳实践
作为非常古老的协议(1971年首次提出,1980年首次应用),FTP在目前依然占有一席之地,但是由于目前IT业发展的非常迅猛,它和现在的基础架构产生了一些水土不服,本文旨在通过日常常见的案例,来介绍如何在阿里云上搭建FTP Server和FTP Client。
6312 0