FTP服务器与客户端设计与开发Word格式.doc
《FTP服务器与客户端设计与开发Word格式.doc》由会员分享,可在线阅读,更多相关《FTP服务器与客户端设计与开发Word格式.doc(32页珍藏版)》请在冰豆网上搜索。
各种类的功能:
1.CFTPServer类:
是CWnd的子类,作为程序的顶层类,负责实现或者调用各个成员函数
2.CApplicationDlg类:
CDialog类的子类,实现程序主窗口。
3.CListenSocket类:
负责监听FTP客户端连接,并实现有效连接
4.CConnectThread类:
负责实现并保证多个连接的有效性。
5.CConnectSocket类:
实现FTP命令的解析,数据的发送和接收
CFTPServer类
作为服务器的顶层类,实现服务器开始运行时的所有成员函数
申明如下:
classCFTPServer:
publicCWnd
{
friendCConnectSocket;
//CConnectSocket作为其友元类,可以访问内部私有数据成员
public:
voidSetGoodbyeMessage(LPCTSTRlpszText);
//发送退出信息
voidSetWelcomeMessage(LPCTSTRlpszText);
//发送欢迎信息
voidSetTimeout(intnValue);
//设置暂停时间
voidSetPort(intnValue);
//设置端口
voidSetMaxUsers(intnValue);
//设置最大连接数
voidSetStatisticsInterval(intnValue);
//统计时间间隔
BOOLIsActive();
//是否有效
voidStop();
BOOLStart();
CFTPServer();
virtual~CFTPServer();
CUserManagerm_UserManager;
//用户管理对象
CSecurityManagerm_SecurityManager;
//安全策略
最主要的成员函数是start()和stop(),分别负责ftp服务器的开始运行和结束运行
函数声明如下:
/********************************************************************/
/* */
/*Functionname:
Start */
/*Description:
Startlistiningonport21andacceptnew */
/* connections. */
BOOLCFTPServer:
:
Start()
if(m_bRunning)
returnFALSE;
//如果运行,返回错误标志
//createdummywindowformessagerouting
if(!
CWnd:
CreateEx(0,AfxRegisterWndClass(0),"
FTPServerNotificationSink"
WS_POPUP,0,0,0,0,NULL,0))
{
AddTraceLine(0,"
Failedtocreatenotificationwindow."
);
}
//开始创建socket
if(m_ListenSocket.Create(m_nPort))
//startlistening
if(m_ListenSocket.Listen())
{
m_ListenSocket.m_pWndServer=this;
m_bRunning=TRUE;
SetTimer(1,m_nStatisticsInterval,NULL);
AddTraceLine(0,"
FTPServerstartedonport%d."
m_nPort);
returnTRUE;
}
AddTraceLine(0,"
FTPServerfailedtolistenonport%d."
//destroynotificationwindow
if(IsWindow(m_hWnd))
DestroyWindow();
m_hWnd=NULL;
returnFALSE;
}
Stop */
StopFTPserver. */
voidCFTPServer:
Stop()
if(!
m_bRunning)
return;
//stopstatisticstimer
KillTimer
(1);
m_bRunning=FALSE;
m_ListenSocket.Close();
CConnectThread*pThread=NULL;
//closeallrunningthreads
do
m_CriticalSection.Lock();
POSITIONpos=m_ThreadList.GetHeadPosition();
if(pos!
=NULL)
pThread=(CConnectThread*)m_ThreadList.GetAt(pos);
m_CriticalSection.Unlock();
//savethreadmembers
intnThreadID=pThread->
m_nThreadID;
HANDLEhThread=pThread->
m_hThread;
[%d]Shuttingdownthread..."
nThreadID);
//tellthreadtostop
pThread->
SetThreadPriority(THREAD_PRIORITY_HIGHEST);
PostThreadMessage(WM_QUIT,0,0);
//waitforthreadtoend,whilekeepingthemessagespumping(max5seconds)
if(WaitWithMessageLoop(hThread,5000)==FALSE)
{
//threaddoesn'
twanttostopped
AddTraceLine(0,"
[%d]Problemwhilekillingthread."
//don'
ttryagain,soremove
m_CriticalSection.Lock();
POSITIONrmPos=m_ThreadList.Find(pThread);
if(rmPos!
m_ThreadList.RemoveAt(rmPos);
m_CriticalSection.Unlock();
}
else
[%d]Threadsuccessfullystopped."
else
pThread=NULL;
while(pThread!
=NULL);
FTPServerstopped."
CListenSocket类
用于监听每个客户的连接,CListenSocket类是CAsyncSocket的子类,其成员函数listen监听来自客户端的连接,当监听到可以接收的socket的时候通过OnAccept函数准备创建有效连接的进程。
函数如下:
voidCListenSocket:
OnAccept(intnErrorCode)
//Newconnectionisbeingestablished
CSocketsockit;
//AccepttheconnectionusingatempCSocketobject.
Accept(sockit);
//Createathreadtohandletheconnection.Thethreadiscreatedsuspendedsothatwecan
//setvariablesinCConnectThreadbeforeitstartsexecuting.
CConnectThread*pThread=(CConnectThread*)AfxBeginThread(RUNTIME_CLASS(CConnectThread),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pThread)
sockit.Close();
TRACE("
Couldnotcreatethread\n"
CFTPServer*pWnd=(CFTPServer*)m_pWndServer;
//sinceeverythingissuccessful,addthethreadtoourlist
pWnd->
m_CriticalSectio