When should I delete ServerSocket when client shutdown
-
When client shutdown,it will send a FD_CLOSE to server and the server receive this message,then the server invoke the function OnClose().In this function I want to destruct the server socket object.BUt how can I? In my program(it's a dialog based program with socket support),the var m_ConnectList used to record how many ServerSocket have been create.
void CBBSServerDlg::ClientConnect() { m_pServer=new CServeSocket(); if(m_Listen.Accept(*m_pServer)) { m_ConnectList.AddTail(m_pServer); m_pServer->Init(this); UpdateData(FALSE); } else { delete m_pServer; m_pServer=NULL; } }
this function will be called by a CListenSocket when it received a FD_ACCEPT. So I want to destruct the serversocket object when client shutdown with the following codevoid CBBSServerDlg::LostClient(CServeSocket* pSocket) { ASSERT(pSocket!=NULL); POSITION pos=m_ConnectList.Find(pSocket,NULL); if(pos!=NULL) { m_ConnectList.RemoveAt(pos); } pSocket->Close(); delete pSocket; pSocket=NULL; }
And of course the function is called by ServerSocket when received a FD_CLOSE.void CServeSocket::OnClose(int nErrorCode) { m_pMainDlg->LostClient(this); CSocket::OnClose(nErrorCode); }
Now I have a question that if I call the function LostClient,the ServerSocket will be destroyed ,and the where the function returned. But if I don't do it this way,How can I destroy the ServerSocket object when client shutdown Thank you very much for your help! Don't look at me in that way! -
When client shutdown,it will send a FD_CLOSE to server and the server receive this message,then the server invoke the function OnClose().In this function I want to destruct the server socket object.BUt how can I? In my program(it's a dialog based program with socket support),the var m_ConnectList used to record how many ServerSocket have been create.
void CBBSServerDlg::ClientConnect() { m_pServer=new CServeSocket(); if(m_Listen.Accept(*m_pServer)) { m_ConnectList.AddTail(m_pServer); m_pServer->Init(this); UpdateData(FALSE); } else { delete m_pServer; m_pServer=NULL; } }
this function will be called by a CListenSocket when it received a FD_ACCEPT. So I want to destruct the serversocket object when client shutdown with the following codevoid CBBSServerDlg::LostClient(CServeSocket* pSocket) { ASSERT(pSocket!=NULL); POSITION pos=m_ConnectList.Find(pSocket,NULL); if(pos!=NULL) { m_ConnectList.RemoveAt(pos); } pSocket->Close(); delete pSocket; pSocket=NULL; }
And of course the function is called by ServerSocket when received a FD_CLOSE.void CServeSocket::OnClose(int nErrorCode) { m_pMainDlg->LostClient(this); CSocket::OnClose(nErrorCode); }
Now I have a question that if I call the function LostClient,the ServerSocket will be destroyed ,and the where the function returned. But if I don't do it this way,How can I destroy the ServerSocket object when client shutdown Thank you very much for your help! Don't look at me in that way! -
I am not familiar with winsock wrapper classes. Under raw winsock, one solution is closesocket(). Kuphryn
As a matter of fact,I just want to know the things following. I have two classes
claas B; class A { ....... void create(); void destroy(B *pB) } clss B { void SetAttach(A * pA){m_pA=pA;} A m_pA; } ....... A::Create() { B b; } A::Destroy(B *pB) { pB->destroy(); delete pB; pB=NULL; }
and if I invoke A::Destroy(B *pB)from class Bb::someFunction() { m_pA->Destroy(this); }
I think it will be throw out a error because the function can't be returned when pB was deleted. So I want to get a useful method to delete pB For my poor,wish you can understand me! Don't look at me in that way!