socket通讯,高手帮忙了!
现在我做个socket通过udp方式进行通讯,
现在有问题,我将socket在线程中创建后,
socket就接收不到发送给的消息,
同样的代码,在主框架窗口创建后就可以!
问一下高手,如何解决?是现在线程中创建socket并且能够
接收到消息?高手帮忙啊!
socket的创建方式如下:
class CUdpSvrSocket : public CAsyncSocket
{
// Attributes
public:
// Operations
public:
CUdpSvrSocket();
virtual ~CUdpSvrSocket();
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUdpSvrSocket)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL
// Generated message map functions
//{{AFX_MSG(CUdpSvrSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
// Implementation
protected:
};
线程创建如下:
UINT LineThreadProc(LPVOID pParam)
{
// volatile int nTemp;
CoInitialize(NULL);
::WaitForSingleObject(g_eventStart, INFINITE);
CUdpSvrSocket * pListen;
pListen = new CUdpSvrSocket();
if(pListen->Create(7701,SOCK_DGRAM))
{
while(1)
{
if (::WaitForSingleObject(g_eventKill, 0) == WAIT_OBJECT_0)
{
break;
}
}
}
return 0; // ends the thread
}