我的问题是UDP通信,使用m_sockRecv.ReceiveFrom(szRecv,50,szIP,uPort,0);接收,ReceiveFrom(为阻塞),在没有接收到数据时,其他操作会被挂起,会导致程序卡死,比如MFC窗口无法拖动等。所以我开了一个线程去执行ReceiveFrom(),但编译没问题,程序运行时报错如下:Debug Assertion Failed ...sockcore.cpp
找出原因是这句:int iRecv=pDlg->m_sockRecv.ReceiveFrom(szRecv,50,szIP,uPort,0);,请教大神们,该如何解决?????谢谢。
源代码如下:
DWORD __stdcall ThreadProc(LPVOID lpParameter)
{
CBroadcastClientDlg* pDlg = (CBroadcastClientDlg*)lpParameter;
CString str;
char szRecv[50];
CString szIP("192.168.43.200");
UINT uPort=8552;
//接收UDP数据
while(1)
{ int iRecv=pDlg->m_sockRecv.ReceiveFrom(szRecv,50,szIP,uPort,0);
//pDlg->MessageBox("连接失败"); //可以执行
TRACE("received %d byte\n",iRecv);
szRecv[iRecv]='\0';
str=szRecv;
pDlg->m_ctrlEDIT1.SetWindowText(str);
pDlg->m_ctrlEDIT1.Invalidate();
/* for (int i=0; i<99999;i++) //单线程计数,用于验证多线程
{
str.Format("%d",i);
pDlg->m_ctrlEDIT1.SetWindowText(str);
pDlg->m_ctrlEDIT1.Invalidate();
} */
/* CBroadcastClientDlg* pDlg = (CBroadcastClientDlg*)lpParameter; //单线程进度条,用于验证单线程
pDlg->m_ctrlProg.SetRange32(0,999999);
for (int i=0; i<999999;i++)
{
pDlg->m_ctrlProg.SetPos(i);
}*/
}
return 0;
}
void CBroadcastClientDlg::OnOK()
{
// TODO: Add extra validation here
m_hThread = CreateThread(NULL,0,ThreadProc,this,0,NULL);
// m_hThread= (HANDLE)_beginthreadex(NULL,0,ThreadProc,this,0,NULL);
}