Ccontrolsocket Class of analysis, the content of Ccontrolsocket class more, why? Because the transmission of communication control commands is all here, the diversity of communication protocols also leads to the diversity of protocol resolution.
1, onreceive Its general description: This function is called by the framework, notifies the socket buffer that there is data, you can call the receive function to take out.
1 voidCcontrolsocket::onreceive (intNerrorcode)2 {3 Try4 {5TCHAR buff[buffer_size+1];6 7 intNread =Receive (Buff, buffer_size);8 Switch(nread)9 {Ten Case 0: One Close (); A Break; - - CaseSocket_error: the if(GetLastError ()! =wsaewouldblock) - { -TCHAR szerror[ the]; -wsprintf (Szerror,"onreceive Error:%d", GetLastError ()); + AfxMessageBox (szerror); - } + Break; A at default: - if((M_rxbuffer.getlength () + nread) >Buffer_overflow) - { -((Cclientthread *) m_pthread)->poststatusmessage ("Buffer overflow:dos attack?"); - - //buffer Overflow (DOS attack?) inAfxgetthread ()->postthreadmessage (Wm_quit,0,0); - } to Else + if(Nread! = Socket_error && Nread! =0) - { the //terminate the string *Buff[nread] =0; $M_rxbuffer + =CString (buff);Panax Notoginseng - GetCommandLine (); the } + Break; A } the } + Catch(...) - { $ //something bad happened ... (DOS attack?) $((Cclientthread *) m_pthread)->poststatusmessage ("Exception occurred in csocket::onreceive ()!"); - //close the connection. -Afxgetthread ()->postthreadmessage (Wm_quit,0,0); the } - csocket::onreceive (nerrorcode);Wuyi}
This function is the Read Control command socket and the corresponding error handling. The received data is saved in the M_rxbuffer. Next, the GetCommandLine function parses the received data.
2. getcommandline function Parsing command data
1 voidCcontrolsocket::getcommandline ()2 {3 CString strtemp;4 intNIndex;5 6 while(!m_rxbuffer.isempty ())//there is received data to be processed7 {8NIndex = M_rxbuffer.find ("\ r \ n");//to find a complete command of the Terminator9 if(NIndex! =-1)Ten { Onestrtemp = M_rxbuffer.left (NIndex);//to extract this command. AM_rxbuffer = M_rxbuffer.mid (NIndex +2);//Update M_rxbuffer Remove commands that have been extracted - if(!strtemp.isempty ()) - { theM_strcommands.addtail (strtemp);//It is possible to extract multiple commands from the while loop, where a queue is added - //parse and execute command -ProcessCommand ();//to handle these commands, if the command is processed directly, there is no above M_strcommandsz this queue buffer - } + } - Else + Break; A } at}
The explanation is already on the top. Cstring::mid's function is somewhat different from what I remember, and is deliberately queried. CString CString:: Mid (int nfirst) const
Return value: Returns a CString object that contains a copy of the specified range character. Nfirst The zero-based index of the first character of the substring to be extracted in this CString object.
Analysis of VC FTP server program (iii)