FTP
详细设计
服务器与客户端设计与开发
程序包括5个主要功能:
1.服务器的运行:启动和停止FTP服务
2.用户管理:添加用户,删除用户和设置用户权限 3.服务器配置:设置服务器开放端口,最大连接数等
4.运行统计:统计当前服务器运行时期上传下载的流量等等 5.安全设置:允许连接服务器的IP列表,以及禁止访问的IP
服务器的运行模块
功能:负责FTP服务器的运行。
使用类:CFTPServer类,CApplicationDlg类,CListenSocket类,CConnectThread类,CConnectSocket类 各种类的功能:
1.CFTPServer类:是CWnd的子类,作为程序的顶层类,负责实现或者调用各个成员函数 2.CApplicationDlg类:CDialog类的子类,实现程序主窗口。 3.CListenSocket类:负责监听FTP客户端连接,并实现有效连接 4.CConnectThread类:负责实现并保证多个连接的有效性。 5.CConnectSocket类:实现FTP命令的解析,数据的发送和接收
CFTPServer类
作为服务器的顶层类,实现服务器开始运行时的所有成员函数 申明如下:
class CFTPServer : public CWnd {
friend CConnectSocket;//CConnectSocket作为其友元类,可以访问内部私有数据成员
public:
void SetGoodbyeMessage(LPCTSTR lpszText);//发送退出信息 void SetWelcomeMessage(LPCTSTR lpszText);//发送欢迎信息 void SetTimeout(int nValue);//设置暂停时间 void SetPort(int nValue);//设置端口
void SetMaxUsers(int nValue);//设置最大连接数
void SetStatisticsInterval(int nValue);//统计时间间隔 BOOL IsActive();//是否有效 void Stop(); BOOL Start(); CFTPServer();
virtual ~CFTPServer();
CUserManager m_UserManager;//用户管理对象 CSecurityManager m_SecurityManager;//安全策略
CFTPServer类
最主要的成员函数是start()和stop(),分别负责ftp服务器的开始运行和结束运行 函数声明如下:
/********************************************************************/ /* */ /* Function name : Start */ /* Description : Start listining on port 21 and accept new */ /* connections. */ /* */ /********************************************************************/ BOOL CFTPServer::Start() {
if (m_bRunning)
return FALSE;//如果运行,返回错误标志 // create dummy window for message routing if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), \Server Notification Sink\WS_POPUP, 0,0,0,0, NULL, 0)) {
AddTraceLine(0, \ return FALSE; }
// 开始创建socket
if (m_ListenSocket.Create(m_nPort)) {
// start listening
if (m_ListenSocket.Listen()) {
m_ListenSocket.m_pWndServer = this; m_bRunning = TRUE;
SetTimer(1, m_nStatisticsInterval, NULL);
AddTraceLine(0, \ return TRUE; } }
AddTraceLine(0, \
// destroy notification window if (IsWindow(m_hWnd)) DestroyWindow(); m_hWnd = NULL;
return FALSE; }
/********************************************************************/ /* */ /* Function name : Stop */ /* Description : Stop FTP server. */ /* */ /********************************************************************/ void CFTPServer::Stop() {
if (!m_bRunning) return;
// stop statistics timer KillTimer(1);
m_bRunning = FALSE; m_ListenSocket.Close();
CConnectThread* pThread = NULL;
// close all running threads do {
m_CriticalSection.Lock();
POSITION pos = m_ThreadList.GetHeadPosition(); if (pos != NULL) {
pThread = (CConnectThread *)m_ThreadList.GetAt(pos);
m_CriticalSection.Unlock();
// save thread members
int nThreadID = pThread->m_nThreadID; HANDLE hThread = pThread->m_hThread;
AddTraceLine(0, \
// tell thread to stop
pThread->SetThreadPriority(THREAD_PRIORITY_HIGHEST); pThread->PostThreadMessage(WM_QUIT,0,0);
// wait for thread to end, while keeping the messages pumping (max 5 seconds)
if (WaitWithMessageLoop(hThread, 5000) == FALSE) {
// thread doesn't want to stopped
AddTraceLine(0, \Problem while killing thread.\nThreadID); // don't try again, so remove m_CriticalSection.Lock();
POSITION rmPos = m_ThreadList.Find(pThread); if (rmPos != NULL)
m_ThreadList.RemoveAt(rmPos); m_CriticalSection.Unlock(); } else {
AddTraceLine(0, \ } } else {
m_CriticalSection.Unlock(); pThread = NULL; } }
while (pThread != NULL);
AddTraceLine(0, \
if (IsWindow(m_hWnd)) DestroyWindow();
m_hWnd = NULL; }
CListenSocket类
用于监听每个客户的连接,CListenSocket类是CAsyncSocket的子类,其成员函数listen监听来自客户端的连接,当监听到可以接收的socket的时候通过OnAccept函数准备创建有效连接的进程。 函数如下:
void CListenSocket::OnAccept(int nErrorCode) {
// New connection is being established
相关推荐: