设为首页 收藏本站
查看: 869|回复: 0

[经验分享] A Complete FTP Server

[复制链接]

尚未签到

发表于 2016-6-8 11:40:21 | 显示全部楼层 |阅读模式
  

<!-- Main Page Contents Start --><!-- Article Starts -->


  • Download executable - 45.4 Kb

  • Download source - 93.8 Kb
DSC0000.jpg

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:


  • 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.
  • Initialize the eventsink.
  • Start the server.

DSC0001.gif Collapse DSC0002.png 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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227827-1-1.html 上篇帖子: ftp工具工具类 下篇帖子: FTP断点上传Telnet实现
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表