Winsock 作为一个线程中的 接收问题,总是出错(sockcore.cpp 336 lines),请帮忙

sscchh2000 2003-11-25 10:35:34
我把我的CSocket的接收数据作为一个独立的线程来接收数据,以下是我的线程函数,我用过:AfxBeginThread(ReceiveThread,this);及 CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReceiveThread,(LPVOID)NULL,0,&threadid)两个函数,可仍出现错误,该怎么办呢? 以下是我的线程函数:
DWORD(调用AfxBeginThread返回类型用的是UINT) ReceiveThread(LPVOID param)
{

BOOL Done=FALSE;
int nRead;
int i=0,j=0;
CSocket sockServer;
CSocket sockReceive;
memset(data,0,sizeof(data));
memset(drawdata,0,sizeof(drawdata));
Sleep(100);
if(!sockServer.Create(8080))return 0;
if(!sockServer.Listen(1))return 0;
if(!sockServer.Accept(sockReceive))return 0;;
while(!Done)
{
nRead=sockReceive.Receive(data,sizeof(data));
if(nRead==SOCKET_ERROR||nRead==0)Done=TRUE;
while(data[j].id!=0)
{
drawdata[i].fx=data[j].fx;//drawdata[i].f
drawdata[i].fy=data[j].fy;//drawdata[i].f
i++;j++;
}
j=0;
memset(data,0,sizeof(data));
continue;
}
sockServer.Close();
sockReceive.Close();
return 1;
}
经过我调试,发现 if(!sockServer.Create(8080))return 0;出现一下错误:Unhandled exception in my.exe:0xC0000005:Access Violation,请各位高手帮忙,我不甚感激!

注:如果不把接收数据这部分放在一个线程中(采用的是阻塞模式),不出现错误,可是应用程序就会被挂起,不能作其他的工作。
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tsdeng 2003-12-02
  • 打赏
  • 举报
回复
我以前也遇到过在线程中使用CSocket出错的问题,你看看下面的msdn内容,或许
是这个问题。

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


© Microsoft Corporation 1998, All Rights Reserved.
Contributions by Isaac L. Varon, Microsoft Corporation


Additional query words: LookupHandle CAsyncSocket AttachHandle GetValueAt assert wincore.cpp 980 Application Error 0xc0000005 m_pHashTable CSocket access violation debug assertion failed

Keywords : kbMFC kbVC600bug kbWinsock kbVS600sp3fix kbGrpDSMFCATL kbNoUpdate
Issue type : kbbug
Technology : kbAudDeveloper kbMFC

awjx 2003-11-26
  • 打赏
  • 举报
回复
我用的是C#
不知VC6是怎么样的!
public int Login()
{
try
{
connectDone.Reset();
_Socket.BeginConnect(server,new AsyncCallback(ConnectCallback),_Socket);
connectDone.WaitOne();
return 0;
}
catch(Exception e)
{
throw e;
}
}

private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket) ar.AsyncState;
client.EndConnect(ar);
connectDone.Set();
}
catch (Exception e)
{
throw e;
}
}
lucky2all 2003-11-26
  • 打赏
  • 举报
回复
socket有基于消息和事件的,
CSocket是基于消息的,需要窗口这一载体,在线程中创建,因为窗口句柄为空而引出其他错误。

建议用api
awjx 2003-11-25
  • 打赏
  • 举报
回复
你可以试试另一种方法呀1
就是异步编程呀,同样是非阻塞的。
sscchh2000 2003-11-25
  • 打赏
  • 举报
回复
异步编程是怎么回事啊?能不能给一个简单的小程序看看啊?先说声谢谢了!

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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