关于mfc下多线程socket出错
2009-09-10 15:25
387 查看
问?:
我用CSocket写通信程序,但执行到下面的函数时总出错,是什么原因?
是多线程通信,每个线程创建了一个套接字:CMySocket *m_pClientSocket = New CMySocket;
然后在OnSendSocket函数中调用:m_pClientSocket->Send(str,len);
如果消息很少,或者中间有Sleep,则没有错误,否则必然出现ASSERT(pState->m_hSocketWindow != NULL);的错误。
听说
1 是一个Bug,我用VC 5.0也打了sp5,
2 同一个套接字不能在不同的线程传输,但是我每一个线程创建了一个单独的套接字
3 没有调用AfxSockInit(),但是我在每个线程的初始化中都调用了这个函数。
现在实在没有办法解决,请大家帮忙,感谢!
下面的函数是 .../MFC/SRC/SOCKCORE.CPP 里的函数。
BOOL CSocket::PumpMessages(UINT uStopFlag)
{
// The same socket better not be blocking in more than one place.
ASSERT(m_pbBlocking == NULL);
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
ASSERT(pState->m_hSocketWindow != NULL); //这句出错这句出错这句出错这句出错
BOOL bBlocking = TRUE;
m_pbBlocking = &bBlocking;
答:(from microsoft)
Most frequently, this problem is due to the sharing of
CSocket objects between multiple threads.
A CSocket object should be
used only in the context of a single thread because the SOCKET handle
encapsulated by a CAsyncSocket object is stored in a per-thread handle map.
(CSocket is derived from CAsyncSocket.) Other information is stored on a
per-thread basis, including a hidden notification window that MFC uses for
socket notifications.
The assertion failure line, which can be found
in Sockcore.cpp in the /Msvc20/Mfc/Src directory, is:
This assertion failure occurs because the CSocket object was either
created or accepted in the context of another thread. The socket notification
window was created in a different thread, and the m_hSocketWindow for the
current thread is NULL, thus the assertion failure.
我用CSocket写通信程序,但执行到下面的函数时总出错,是什么原因?
是多线程通信,每个线程创建了一个套接字:CMySocket *m_pClientSocket = New CMySocket;
然后在OnSendSocket函数中调用:m_pClientSocket->Send(str,len);
如果消息很少,或者中间有Sleep,则没有错误,否则必然出现ASSERT(pState->m_hSocketWindow != NULL);的错误。
听说
1 是一个Bug,我用VC 5.0也打了sp5,
2 同一个套接字不能在不同的线程传输,但是我每一个线程创建了一个单独的套接字
3 没有调用AfxSockInit(),但是我在每个线程的初始化中都调用了这个函数。
现在实在没有办法解决,请大家帮忙,感谢!
下面的函数是 .../MFC/SRC/SOCKCORE.CPP 里的函数。
BOOL CSocket::PumpMessages(UINT uStopFlag)
{
// The same socket better not be blocking in more than one place.
ASSERT(m_pbBlocking == NULL);
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
ASSERT(pState->m_hSocketWindow != NULL); //这句出错这句出错这句出错这句出错
BOOL bBlocking = TRUE;
m_pbBlocking = &bBlocking;
答:(from microsoft)
Most frequently, this problem is due to the sharing of
CSocket objects between multiple threads.
A CSocket object should be
used only in the context of a single thread because the SOCKET handle
encapsulated by a CAsyncSocket object is stored in a per-thread handle map.
(CSocket is derived from CAsyncSocket.) Other information is stored on a
per-thread basis, including a hidden notification window that MFC uses for
socket notifications.
The assertion failure line, which can be found
in Sockcore.cpp in the /Msvc20/Mfc/Src directory, is:
ASSERT(pThreadState->m_hSocketWindow != NULL);
This assertion failure occurs because the CSocket object was either
created or accepted in the context of another thread. The socket notification
window was created in a different thread, and the m_hSocketWindow for the
current thread is NULL, thus the assertion failure.
相关文章推荐
- 关于多线程中传递MFC窗口类指针时ASSERT_VALID出错的另类解决 .
- 关于多线程中传递MFC窗口类指针时ASSERT_VALID出错的另类解决
- MFC关于多线程中传递窗口类指针时ASSERT_VALID出错的另类解决 转
- (他山之石)MFC学习之路(三)关于编译【windows核心编程】01-ShowError 源代码出错 ,解决方法!
- 关于MFC下多线程,在线程中创建非模态对话框以及消息传递
- 易语言----关于超级网页访问模块 在多线程的使用中 出错的问题 讨论
- 关于socket的多线程操作。
- 关于用MFC创建模态对话框时出错的问题
- 关于MFC中CComboBox变量 运行 出错 Debug Assertion Failed!
- 关于蓝牙socket 服务器端首次accept总出错!
- MFC_SOCKET_关于socket的服务端与客户端的通信
- 关于MFC框架下的多线程
- 关于Socket编程中gethostbyname()出错的原因
- 关于MFC中View类中GetDocument的出错解决方案
- 关于MFC对话框不显示或出错的问题解决总结(更新中。。。)
- 关于蓝牙socket 服务器端首次accept总出错!
- 关于MFC使用和多线程随笔--对初学者可能有点作用
- MFC多线程中创建对话框(create)出错(异常)问题的分析及解决办法
- CWnd::AssertValid()函数执行出错---MFC多线程
- C++ 关于MFC多线程编程的注意事项