Error occured when using CArchive to send Message ?Please do me a favor for it!
-
I am learning using CSocket to send/receive message through net,but here's some problem I use class CSocket CSocketFile CArchive to support my program. At first,I create a new CSocket object and connect to Server,and everything is OK.At the server I can get the connection message from the client. After that,I use the following code to send some message to Server:
CString str; str.Format("%s","risking"); UINT CtrlCode; CtrlCode=USER_NAME; m_pClientSocket->SetSendData(CtrlCode,str,3,1); m_pClientSocket->SendData(); // Sleep(100); CtrlCode=USER_PASSWORD; m_pClientSocket->SetSendData(CtrlCode,str,3,2); m_pClientSocket->SendData(); // Sleep(100); CtrlCode=USER_LOGIN_END; str=""; m_pClientSocket->SetSendData(CtrlCode,str,3,2); m_pClientSocket->SendData();
if I add the two codesleep(100);
in it,everything looks OK at server,I can see the ctrl_code USER_NAME USER_PASSWORD CTL_END at server.But if I delete the twosleep(100)
,I can sometimes see all three ctrl_code,sometimes can only see CTRL_END message. I don't know where is wrong,and the following code is used to send data:void CClientSocket::SendData() { CCommand cmd; cmd.m_CtrlCode=m_CtrlCode; cmd.m_DataStr=m_DataStr; cmd.m_CurrentData=m_CurrentData; cmd.m_TotalData=m_TotalData; cmd.Serialize(*m_pArchiveOut);//m_pFile=new CSocketFile(this); //m_pArchiveOut=new CArchive(m_pFile,CArchive::store); m_pArchiveOut->Flush(); }
At server I use the following code to receive message:void CServeSocket::OnReceive(int nErrorCode) { CCommand cmd; do { cmd.Serialize(*m_pArchiveIn); } while(!m_pArchiveIn->IsBufferEmpty()); UINT CtrlCode=cmd.m_CtrlCode; switch(CtrlCode/100) { case 1:UserLogin(&cmd); ………………. } void CServeSocket::UserLogin(CCommand *pCmd) { switch(pCmd->m_CtrlCode%100) { case 1:m_LogInUserName=pCmd->m_DataStr;m_LoginDataCount++; break; case 2:m_LoginUserPwd=pCmd->m_DataStr;m_LoginDataCount++; break; default:break; } if(m_LoginDataCount>=2)//m_loginDataCount初始化为0 { CCommand cmd; if(m_bLogin=m_DBCtrl.VerifyLogin(m_LogInUserName,m_LoginUserPwd)) { //login successfully cmd.m_CtrlCode=USER_LOGIN_SUCCESSFULLY; cmd.m_DataStr="You a
-
I am learning using CSocket to send/receive message through net,but here's some problem I use class CSocket CSocketFile CArchive to support my program. At first,I create a new CSocket object and connect to Server,and everything is OK.At the server I can get the connection message from the client. After that,I use the following code to send some message to Server:
CString str; str.Format("%s","risking"); UINT CtrlCode; CtrlCode=USER_NAME; m_pClientSocket->SetSendData(CtrlCode,str,3,1); m_pClientSocket->SendData(); // Sleep(100); CtrlCode=USER_PASSWORD; m_pClientSocket->SetSendData(CtrlCode,str,3,2); m_pClientSocket->SendData(); // Sleep(100); CtrlCode=USER_LOGIN_END; str=""; m_pClientSocket->SetSendData(CtrlCode,str,3,2); m_pClientSocket->SendData();
if I add the two codesleep(100);
in it,everything looks OK at server,I can see the ctrl_code USER_NAME USER_PASSWORD CTL_END at server.But if I delete the twosleep(100)
,I can sometimes see all three ctrl_code,sometimes can only see CTRL_END message. I don't know where is wrong,and the following code is used to send data:void CClientSocket::SendData() { CCommand cmd; cmd.m_CtrlCode=m_CtrlCode; cmd.m_DataStr=m_DataStr; cmd.m_CurrentData=m_CurrentData; cmd.m_TotalData=m_TotalData; cmd.Serialize(*m_pArchiveOut);//m_pFile=new CSocketFile(this); //m_pArchiveOut=new CArchive(m_pFile,CArchive::store); m_pArchiveOut->Flush(); }
At server I use the following code to receive message:void CServeSocket::OnReceive(int nErrorCode) { CCommand cmd; do { cmd.Serialize(*m_pArchiveIn); } while(!m_pArchiveIn->IsBufferEmpty()); UINT CtrlCode=cmd.m_CtrlCode; switch(CtrlCode/100) { case 1:UserLogin(&cmd); ………………. } void CServeSocket::UserLogin(CCommand *pCmd) { switch(pCmd->m_CtrlCode%100) { case 1:m_LogInUserName=pCmd->m_DataStr;m_LoginDataCount++; break; case 2:m_LoginUserPwd=pCmd->m_DataStr;m_LoginDataCount++; break; default:break; } if(m_LoginDataCount>=2)//m_loginDataCount初始化为0 { CCommand cmd; if(m_bLogin=m_DBCtrl.VerifyLogin(m_LogInUserName,m_LoginUserPwd)) { //login successfully cmd.m_CtrlCode=USER_LOGIN_SUCCESSFULLY; cmd.m_DataStr="You a
I don't use CSocket but I have in the past. (I ran into tons of problems with it). Anyway as a rule of thumb it's not a good idea to do multiple sends especially with out doing any type of error checking. I'm not 100% sure of the problem but I bet if you run some error checking you will see that the socket is still busy sending the first msg when you try to send the second. That's could be why sleep() allows the socket to finish sending before it attempts the next send. Hope this helps, Rob Whoever said nothing's impossible never tried slamming a revolving door!