请教,我利用MFC的CAsynSocket,派生了服务器端的侦听socket,以及远端客户端的连接来的socket,我在服务器端管理若干个客户端socket,问题是,我在关闭这些socket的时候,程序发生错误,可能原因是正在收发数据。我想问的是怎么正确关闭已经生成的socket对象?程序代码如下:其中,调用pSocket->Close()的时候发生错误。 //清理客户端连接socket
POSITION pos = this->m_ClientInfoMap.GetStartPosition();
CNewSocket *pSocket = NULL;
CString strIPAddr;
for(pos;pos != NULL;)
{
////////////
this->m_ClientInfoMap.GetNextAssoc(pos,strIPAddr,(void *&)pSocket);
if(pSocket)
{
pSocket->ShutDown(2);
//..........................
pSocket->Close();
delete pSocket;
pSocket = NULL;
}
}
this->m_ClientInfoMap.RemoveAll();
//
//删除侦听socket
if(m_pLisener->m_hSocket != INVALID_SOCKET)
{
//
m_pLisener->ShutDown(2);
int nError = ::WSAGetLastError();
//..............................................
m_pLisener->Close();
delete m_pLisener;
}
POSITION pos = this->m_ClientInfoMap.GetStartPosition();
CNewSocket *pSocket = NULL;
CString strIPAddr;
for(pos;pos != NULL;)
{
////////////
this->m_ClientInfoMap.GetNextAssoc(pos,strIPAddr,(void *&)pSocket);
if(pSocket)
{
pSocket->ShutDown(2);
//..........................
pSocket->Close();
delete pSocket;
pSocket = NULL;
}
}
this->m_ClientInfoMap.RemoveAll();
//
//删除侦听socket
if(m_pLisener->m_hSocket != INVALID_SOCKET)
{
//
m_pLisener->ShutDown(2);
int nError = ::WSAGetLastError();
//..............................................
m_pLisener->Close();
delete m_pLisener;
}
解决方案 »
- 包含头文件有先后顺序吗?
- 【求助】win2k3下调用DLL中的对话框失败,win2k、xp中运行正常
- 为什么OnGetMinMaxInfo()不能设置多文档程序中子窗口最大化时的位置
- socket传输结构体问题
- 在用户模式下用api hook 防止外挂使用的一些函数,有什么难点和缺点?
- 请教一个CJ60界面库的问题
- 请问我该用哪种IPC机制?
- 愚人求教(MFC ODBC)
- 一个古老的问题:真不好意思又拿出来麻烦大家一下,各位兄弟帮帮忙呀!
- 如何将程序登记成windows系统服务?
- 我建立了一个单文档的工程,怎么让它一运行,窗口就在屏幕正中间?
- 最近看了看MFC/WTL等切分窗口,感觉flashget的切分界面做的真棒!
CAsyncSocket::DoCallBack(unsigned int 525812, long 1) line 527 + 12 bytes
CSocket::ProcessAuxQueue() line 822
CSocketWnd::OnSocketNotify(unsigned int 525812, long 1) line 1126
CWnd::OnWndMsg(unsigned int 883, unsigned int 525812, long 1, long * 0x0012fd10) line 1815 + 17 bytes
CWnd::WindowProc(unsigned int 883, unsigned int 525812, long 1) line 1585 + 30 bytes
AfxCallWndProc(CWnd * 0x0149be30 {CSocketWnd hWnd=0x000b02da}, HWND__ * 0x000b02da, unsigned int 883, unsigned int 525812, long 1) line 215 + 26 bytes
AfxWndProc(HWND__ * 0x000b02da, unsigned int 883, unsigned int 525812, long 1) line 368
AfxWndProcBase(HWND__ * 0x000b02da, unsigned int 883, unsigned int 525812, long 1) line 220 + 21 bytes
USER32! 77e2158f()
USER32! 77e21dc9()
USER32! 77e07ed6()
CWinThread::Run() line 480 + 11 bytes
CWinApp::Run() line 400
AfxWinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x001336f1, int 1) line 49 + 11 bytes
WinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x001336f1, int 1) line 30
WinMainCRTStartup() line 330 + 54 bytes
KERNEL32! 77e88989()
So your CSocket hwnd is stored in the thread where you invoked Create, and CSocket looks it up in another where you invoke Receive. That is the reason for ASSERT(pState->m_hSocketWindow != NULL);
直接用CloseSocket(pSocket->m_hSocket)