线程中第一次调CSocket::Create()成功,第二次就错误?

chmodyou 2004-07-26 12:59:52
线程中第一次调CSocket::Create()成功,第二次就错误?

调用前都执行AfxSocketInit(),且成功,退出时都有WSACleanup()。

第一次没问题,第二次就出异常,用WSAGetLastError()得到的错误号是1410

应该如何解决?在线等。
...全文
178 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwonzor 2004-07-26
  • 打赏
  • 举报
回复
微软的东西真是烂啊
在CSocket::Create之前加入SocketThreadInit()这样一个函数;

void SocketThreadInit()
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()

_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState->m_pmapSocketHandle == NULL)
pState->m_pmapSocketHandle = new CMapPtrToPtr;
if (pState->m_pmapDeadSockets == NULL)
pState->m_pmapDeadSockets = new CMapPtrToPtr;
if (pState->m_plistSocketNotifications == NULL)
pState->m_plistSocketNotifications = new CPtrList;

#endif
}
ppcust 2004-07-26
  • 打赏
  • 举报
回复
看看能不能利用多线程来解决
gracezhu 2004-07-26
  • 打赏
  • 举报
回复
FIX: Unhandled Exception Using MFC Sockets in Visual C++ 6.0 (Q193101)

--------------------------------------------------------------------------------
The information in this article applies to:


The Microsoft Foundation Classes (MFC), used with:
Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
Microsoft Visual C++, 32-bit Professional Edition, version 6.0
Microsoft Visual C++, 32-bit Learning Edition, version 6.0


--------------------------------------------------------------------------------


SYMPTOMS
When using MFC sockets in secondary threads in a statically linked MFC Visual C++ 6.0 application, an unhandled exception occurs.



CAUSE
The reason for the unhandled exception is that an object of type CMapPtrToPtr pointer, pointed to by m_pmapSocketHandle, is never created.



RESOLUTION
The handle maps used by the sockets need to be created for each thread. The following code shows a function to do this:

void SocketThreadInit()
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()

_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState->m_pmapSocketHandle == NULL)
pState->m_pmapSocketHandle = new CMapPtrToPtr;
if (pState->m_pmapDeadSockets == NULL)
pState->m_pmapDeadSockets = new CMapPtrToPtr;
if (pState->m_plistSocketNotifications == NULL)
pState->m_plistSocketNotifications = new CPtrList;

#endif
}
This function should be called once in each secondary thread before the first socket is created in the new thread.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

This bug was corrected in Visual Studio 6.0 Service Pack 3. You will need to call AfxSocketInit() in each thread that uses sockets.

For more information about Visual Studio service packs, please see the following articles in the Microsoft Knowledge Base:



Q194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why
Q194295 HOWTO: Tell That Visual Studio 6.0 Service Packs Are Installed



MORE INFORMATION
In MFC socket applications, AfxSocketInit is called once, by default, in the primary thread. This function creates the handle maps for the primary thread when statically linked to MFC. However, when a secondary thread is created, these per-thread handle maps are not created. AfxSocketInit must be called in each thread to initialize the socket libraries.

Steps to Reproduce Behavior
Create an application that creates a socket in a secondary thread and uses MFC in a static link library. You can use the MultiSoc sample described in the following Knowledge Base article by changing the project settings to use MFC in a static library:


Q175668 FILE: MultiSoc: Illustrates Using Sockets in Multiple Threads
Run the application and create a socket in a secondary thread.





REFERENCES
For additional information, please see the following article in the Microsoft Knowledge Base:

Q175668 FILE: MultiSoc: Illustrates Using Sockets in Multiple Threads
gracezhu 2004-07-26
  • 打赏
  • 举报
回复
CSocket只能在GUI线程中create
chmodyou 2004-07-26
  • 打赏
  • 举报
回复
这个问题好像很难解决啊
Hawk_lp 2004-07-26
  • 打赏
  • 举报
回复
up

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧