函数ftp服务器与客户端设计与开发

ftp服务器地址  时间:2021-01-30  阅读:()

FTP服务器与客户端设计与开发

详细设计

程序包括5个主要功能

1.服务器的运行:启动和停止FTP服务

2.用户管理添加用户,删除用户和设置用户权限

3.服务器配置设置服务器开放端口,最大连接数等

4.运行统计:统计当前服务器运行时期上传下载的流量等等

5.安全设置允许连接服务器的IP列表以及禁止访问的IP

服务器的运行模块

功能负责F服务器的运行。

使用类 类,CAplic  i Dg类 CListnSocket类 Cnecthread类 ConnctS ockt类

各种类的功能

1.C类是CWnd的子类作为程序的顶层类负责实现或者调用各个成员函数

2.Aplica ionDg类:CDialog类的子类,实现程序主窗口。

3.CLi  tenSoket类负责监听T客户端连接,并实现有效连接

4.ConnectThred类:负责实现并保证多个连接的有效性。

5.CnnectScket类实现FTP命令的解析,数据的发送和接收

类

作为服务器的顶层类实现服务器开始运行时的所有成员函数

申明如下:

lass  :pulic Cd

{fr  ed CCnnec  ockt;/CConnectocke 作为其友元类,可以访问内部私有数据成员publi:

 void StGoodbyeMessage CTSTR pszTxt ;/发送退出信息

 vod SetWl eMessae(LPCTSTR lpszTet ;/发送欢迎信息

oid Setmeout(i  al );设置暂停时间

oid SetPo t(it nValue)  //设置端口vod   xUse  s int nVa ue ;//设置最大连接数

 void StSta isticsInterval(int ale);//统计时间间隔

 O O IsActie();/是否有效

 void S o() 

 BOOL S art );

() vir ualC();

 CU rManaer //用户管理对象

 CSecurityanager;/安全策略

类

最主要的成员函数是start(和stop ) 分别负责ftp服务器的开始运行和结束运行函数声明如下:

/*  ******* ** ****  ********  ***** ******** 

********/

/*        /

*Funtin ame :Start      *

 De  cr ptio :Strt li   inin on  rt 1 an accpt new //*   onnec  ions     */

/        *

** ******************  ****************************

**/

BOOLC()

{if (munnng)

      n FALSE;/如果运行,返回错误标志

/crea  dm wndow for me  sag  u i 

 f(!Cd::Cr   Ex(0 AxRgis  erWndC as s(0),  N  ifica in ink",S_PPUP,   0,0,0 NL,   )

 dTacine  , Fild to create notifia  on w dow.");return FALSE;

/开始创建socketif misteSockt.Cr te m_ort))

{

  //  tr   i  teningi  (mL s  enocket.Listn( )

{

  _ListeSocet._pWdSe ve  = h  

  m_bR ning= T RU;

 S e Timer(  ,m_nStt   ticsInterval,NUL ;

  d;

 }

 

 AdTrceLin(0, " faild to    tn o port%.  ,mPort);

/de  troy no  ficatio indow

 f(IsWnow(m_hWd )

  Destr no();

 mhWnd  N ULL;

 rturn FALE;

}

*  *****************************************

*********

/*          */

/*Fuction ame  Stp     /

*D e  cription  S top .    */

/*         /

/********************************* ********** *************voi C()

 if !m_bunning

 rturn;

// stop sttist  cs timer

Killim e r 1 

_Running FALS E m_i  te ocket.lo ( ;

CnnetThre ad*pThr d=ULL;

//     e ll rnning thred

 d

{

  m_C  ti lec  in.Lock();

 POSITION os _Th ad  s  .GetedPositin();

  (po !=NULL)

 {

   pThead (CConec Thred*)mTreadist.etAt(os) 

 

_C  t  ca  ec  in Un ok( ;

 / save trea membrsin nT hr e aID= pTr  ->_nT he aI

  HN D L hThr ea = T ed-m_hr e a;

   AddTracLine(0, " %d]Sht ing  wn t ed.. .   hreadD   //tll hread to stop

 pThed- threadPr  or  tyTHEAD_PRITYHIHT ;  Thread>PostTh adMssge(_QUI,0,0);

  / wa  t for hrad o end,while keepin te m   ges umping(x 5 scods)

  i  (i WithM   aeo op(hTrad,50)== FALS)

   //

    AdTrceLne(0, "[]Problmhil killig thread.  , nTreadID);

  //dn't t y agai, so remov

   m_r  ticlS ectin.c();

  POSION rmPo m_Tr dList.Fn phread);

 if(rmP o s  =NULL)

  _TheadLis   RmveA (rmPos);

 mritica  ection.Un oc );

  }

   el  

{

   AddTraceLine(0 "[%]Thread scce  sfully stpe." nThredID); }

  }

 else

 {m_Criticaletion.Un o () 

  pThea =NUL;

 

}

hi  e (pT he a != N ULL) 

AdracLne 0, "  topped " 

 if(I  do(hWn))

Ds  r oWino( 

 m_n =NUL;

}

Ci  t Scket类

用于监听每个客户的连接,CLi  teSockt类是CAsyncSockt的子类其成员函数 isten

监听来自客户端的连接,当监听到可以接收的socket的时候通过OAccept函数准备创建有效连接的进程。

函数如下vid CL  tnSoket: :OnAcept  nt nErrrCoe)

/New  nec   on is eing estali he

CSoket scki  

Acept th connct  on sing a te CSckt obj t.

Accept(socki  );

/ Create a tred t hndl te connection. Th t  ad i  reaed suspended so hat we can

/ st ariab e  in onnctTh ead before  t sta   s execut n. CConectTrea  pThread=(onetThrea*)AfxegnThrad(RUNTI_CAS S(ConnctThread), THEAD_IRIY_ORMA 0, CRASUSENED);

  f (!pTh ad)

{socki   lo e() 

 RAE "Could not ra  theadn");

   tun;

}

C*Wn =(C*m_pWnder r;

 ine eve ytig  s suces  fl, dd the thr d t ou li  t Wn->mriticalS e  ion ock  ;pWnd->mThreadList.AddTail(pThead) 

Wnd->m_ri  i lSetion.Un ock( ;

//  ae pointer

 pTrad-WnSr r= Wde ve  ;

 //Pass the socke  to the the    passig he so  t hndle You nnot pa 

/ a Cocketojectacross   eads.

Trea->mS oce  =sockit.et h();

 // w  ta  t th  read.

 pThread>Re umeThread( 

}

CConnecthead类

CCnnectThead类负责为每个有效进程创建一个线程,每个进程完成数据传输的所有任务,穿件县城后通过 itInstance完成线程的初始化

OOL C nectThread :Ii  ns  ance()

{

 try

 {

 //Attac the socke  handle to a Sce  objct.

/ Th  make   re tat te ocke  noti ications are sent to th thread.

 mConnctSockt.ttch(m_hSo e  ) 

  _ConectS ocktm_pThred= his

Strig strIPdd s  ;

IT Por  ;

 m_o ec  ocket GtPeerNam(  trIPAddrss, P  t ;

  /otif se  e  that there

  m_pWnd rvr->ene  sg(_HRASTAR, (WPARA th s,);

 i  (( C*m_W Se ver ->ChecMaUsrs  )

connectd 

 P  ThredMssage(_UIT,0 0);

}

  e  se

 if( (C*)mWdServer -I  IPddre  sA lowd(strIPAddress))

 {

  y te server.");

     tTredMessage(WM_UIT,,);

}e  se

  

 / send wlom messge o client

  C  rin strText =((C* _nd rvr)->Get  cmeessge( ;

;

 m_ime  I =  :SetTmer(UL,0, 000,TmerProc ;

 }

 }

 ath(Exception *e)

 {

 e->Dle  e( ;

}

   urn TRUE;

}

线程结束以后通过Ei  Instnce函数实现资源的释放代码如下:

n CCnnectThread:  xitIstance(

C *pWnd  (C )m_pWndS e ver;try

 {

 pWnd->m_Cr  ti lSc    .Lo ();

/ d  t th   read rom the line li  

 POSIION ps= Wnd-m_TheaL s  .Find(t  s);

    (pos ! NUL)

 {

 pnd>m_radLi   .Rmovt ps);

 }

 pWnd-m_Cri  ialS ec  ion.Unlock(); 

 // notify service main lop

 pWn->SendMe  sge(_HEDCLOS, (WPARAM)this,  

}

 catch(Eception

 {pWd->_  i  iclS ect  on.Unlock() 

 e-Del  e() 

}retur CWinThread: :Eit  stance( ;

}

为了了解传输过程中接收和发送的字节数使用ncRecei  tes和IncSentytes来计算。这两个函数在Connec   ce 类中调用,代码如下:

oid CConctTr d: :IncSntte  in nByte  

m_L  Dtrans erTie = Cie: :Ge urrentTm();

 

 // n  i   serer class

 mnd rer-Ps e s sag WM_THREAMS, (WPARA 0, (LPAAM)nB y e   ;

}

vid CConnectThead  InRce vedByte  (int nytes)

{

 m_a tD ataTras e Tme =Cme:  e CrrentTime();

 _nRceived  e  +=nBytes;

/noti y  erver   ass

 m_pndS erver->Po tes  age(WM_THREADS G, (WPAA)1  (LPARAnBytes) 

CConnectocke 类

每个线程都是通过一个CConetocke 对象m_onectScket来完成数据的接受和发送。当线程创建成功以后m_ConnectS  et对象通过nReceiv函数获得数据,然后利用ParseCommand函数来解析其中FT命令vid Connect cket: :OnRce  e(i  nErrorCode)

 THR bff[BUFFRS ZE ;it nReadReeive(buf, FFERS Z);

wi  ch (Read)

  case 0:

  Close() 

  bek;

 case SCKET_EROR

 if (e LstError(  =WSEWO ULB LO)

 {

  TCA szError[256 ;wpritf srror());  

   fxss eBox(  rror 

 }

  bea;

  efault:

  f(Read  = O C KET_ROR& Read != 0)

 {

   ((ConnetThead*)AfGe Thr d())>InReceivdBy sRed);  /te minate he str  g

   buf[nead]=  

  m_RxB u ff  += S trin(bff) 

  

  e tRLin ) 

  }

 rek;

 }

Cokt: :Onec  e(nErr Cod) 

}

ParseComnd函数

是当前程序最重要的一个部分它根据客户端提交的各种命令进行相应的操作代码如下vid ConnctSck  :  PrsCmad()

{

 sta i C ommanList ]=

 {

  {O KUR  "U S E",RU ,

 {T O_PAS "PAS S  , RUE ,

 _CW  "", TRUE} 

 TOKPWD, "WD", FASE} 

{_OR, POT", TR ,

 {T O K_ASV PAS , FLS E},

 {OK_YP, "TYPE",TRU},

 {TK_LIT, "I", ALE},

  {T_RST, REST",RUE},

  {TO_CDUP  "CDUP"  FALSE ,

 {T O K_ET R  "ETR",T RE},

 {TOK_STOR, "STO  T},

  TOK_IZ "IZ" TRU},

 {O KD ELE, "DL",TRU} 

 TOK_MD  "MD", TRE},

 {K_KD, M KD", RU E},

 {T O K_NR, "RNF",T RU E},

{TK_RTO, RTO",TRU ,

 TOK_ABOR "ABO  ALSE ,

 {T O K_SYST, "SYST", FALE ,

 TOK_NOOP, "OOP , FALS} 

TO_BY,    , FALS ,

{TOKQT, "UT", FASE},

  {TOK_ROR  "", FLS},

 ;

/ parse comman

C  ring strCommnd st Aget  if(!etRxomad(st Cmmand  rArguments))

{

RAKsmart 年中活动 独立服务器限时$30秒杀 VPS主机低至$1.99

RAKsmart 虽然是美国主机商,但是商家的主要客户群还是在我们国内,于是我们可以看到每次的国内节日促销活动期间商家也会发布促销。包括这次年中大促活动,RAKsmart商家也有发布为期两个月的年终活动,其中有商家擅长的独立服务器和便宜VPS主机。服务器包括站群服务器、特价服务器、高达10G带宽不限制流量的美国服务器。商家优惠活动,可以看到对应商品的优惠,同时也可以使用 优惠码 RAKBL9 同时...

ZJI(月付450元),香港华为云线路服务器、E3服务器起

ZJI发布了9月份促销信息,针对香港华为云线路物理服务器华为一型提供立减300元优惠码,优惠后香港华为一型月付仅450元起。ZJI是原来Wordpress圈知名主机商家:维翔主机,成立于2011年,2018年9月更名为ZJI,提供中国香港、台湾、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册等业务,商家所选数据中心均为国内访问质量高的机房和线路,比如香港阿里云、华为...

TNAHosting($5/月)4核/12GB/500GB/15TB/芝加哥机房

TNAHosting是一家成立于2012年的国外主机商,提供VPS主机及独立服务器租用等业务,其中VPS主机基于OpenVZ和KVM架构,数据中心在美国芝加哥机房。目前,商家在LET推出芝加哥机房大硬盘高配VPS套餐,再次刷新了价格底线,基于OpenVZ架构,12GB内存,500GB大硬盘,支持月付仅5美元起。下面列出这款VPS主机配置信息。CPU:4 cores内存:12GB硬盘:500GB月流...

ftp服务器地址为你推荐
绿色桌面背景图片什么类的桌面背景图好看?腾讯空间首页手机QQ空间首页从哪里进入!视频剪辑软件哪个好后期视频剪辑什么软件最专业?英语词典哪个好英语词典哪个好无纺布和熔喷布口罩哪个好一次性口罩的熔喷布和KN95的熔喷布有什么不同?炒股软件哪个好用玩股票哪个软件好?网络机顶盒哪个好哪个品牌的网络机顶盒好用?360云盘关闭360云盘已经关闭了 文件怎么下360云盘共享群手机360云盘怎么入共享群,求步骤便宜摩托车五羊本田最便宜的摩托车
重庆虚拟主机 云南服务器租用 私服服务器租用 美国vps评测 cn域名个人注册 hawkhost 便宜域名 美国翻墙 外国域名 国外空间 远程登陆工具 促正网秒杀 web服务器架设 圣诞促销 新家坡 nerds 免费高速空间 上海联通宽带测速 google台湾 免费邮件服务器 更多