天天看点

A Complete FTP Server

<a href="http://writeblog.csdn.net/ftpserver/ftpserver.zip">Download executable - 45.4 Kb</a>

<a href="http://writeblog.csdn.net/ftpserver/ftpserversrc.zip">Download source - 93.8 Kb</a>

A Complete FTP Server

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 <code>CFTPServer</code> 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.

<code>BOOL Start()</code>

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

<code>void Stop()</code>

Deactivates the server and disconnects all connected clients by terminating the running threads.

<code>BOOL IsActive()</code>

Is FTP server active?

<code>void SetMaxUsers(int nValue)</code>

Set maximum number of users.

<code>void SetPort(int nValue)</code>

Set listening port for new connections.

<code>void SetTimeout(int nValue)</code>

Set connection timeout (in ms). When a client does not send any commands for <code>nValue</code> ms, the server will close the connection.

<code>void SetWelcomeMessage(LPCTSTR lpszText)</code>

Set the text that will be displayed when the client logs on.

<code>void Initialize(CFTPEventSink *pEventSink)</code>

Set the event sink. The event sink will be the window (or any class) that receives the events generated by the FTP server. See <code>CFTPEventSink</code> description for more info.

To be able to 'send' events from the <code>CFTPServer</code> class to the main application, I used multiple inheritance and virtual functions. The <code>CFTPEventSink</code> is just a helper class that contains nothing else than virtual functions, when you derive your class from <code>CFTPEventSink</code> these virtual functions become a kind of events. The class <code>CFTPServer</code> has a reference to this class and calls the virtual functions when it needs to notify the application.

The following 'events' are available:

<code>void OnFTPUserConnected(DWORD nThreadID, LPCTSTR lpszUser, LPCSTR lpszAddress);</code>

A client has successfully connected.

<code>void OnFTPUserDisconnected(DWORD nThreadID, LPCTSTR lpszUser);</code>

A client has disconnected.

<code>void OnFTPStatusChange(int nType, LPCTSTR lpszText);</code>

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

<code>void OnFTPReceivedBytesChange(int nBytes);</code>

The number of received bytes has changed.

<code>void OnFTPSentBytesChange(int nBytes);</code>

The number of sent bytes received has changed.

<code>void OnFTPStatisticChange(int nType, int nValue);</code>

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

The class <code>CUserManager</code> handles all user and file related stuff. It checks the connected users for their access rights and converts remote to local paths. <code>CUserManager</code> uses serializing for storing and loading the user settings.

This socket is part of <code>CFTPServer</code> and accepts incoming connections. When a clients connects to the server, <code>CListenSocket</code> accepts the connection and creates a new thread (<code>CConnectThread</code>) that will take care of all further communication between the client and the server. After the thread has been created, <code>CListenSocket</code> will return to its waiting state.

This thread will handle all communication between the client and the server using <code>CConnectSocket</code>.

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 <code>CDataSocket</code> will be created by <code>CConnectSocket</code>. The <code>CDataSocket</code> 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.

To use the class in your application, you need to do the following:

Add the class to your application.

Derive your main class from <code>CFTPEventSink</code>.

Override the virtual functions of <code>CFTPEventSink</code>; these are the events that come from the server.

Initialize the eventsink.

Start the server.

A Complete FTP Server

Collapse

A Complete FTP Server

<a href="http://writeblog.csdn.net/#">Copy Code</a>

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 href="http://writeblog.csdn.net/Members/Pablo-van-der-Meer">Pablo van der Meer</a>

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

继续阅读