要在程序启动时用Socket访问远程服务器进行登陆检查,但是又要防止超时,为此,不得已采用了多线程(由于我在InitInstance中加入了相关代码,此时消息处理好像还不能起效<猜测而已,没有研究过MFC的实现机制,^_^>),但是,仍然等待超时,不知何故,请各路大侠帮忙症断一下。
我的实现代码如下:
//初始化线程参数,略
...
//启动线程进行数据发送
AfxBeginThread(SendDataProc, &dts);
if (WAIT_OBJECT_0 == WaitForSingleObject(m_eventComm, nTimeOut))
{
if (dts.commStatus == COMM_SUCCEED)
{
return true; //通过检查
}
else
{
AfxMessageBox("用户名或密码不正确!");
continue;
}
}
else
{
dts.bTerminate = true;
AfxMessageBox("服务器处理超时!");
continue;
}以下是工作线程函数代码:
UINT SendDataProc(LPVOID pParam)
{
CString strIP, strPort, strTimeOut;
ASSERT(theApp.GetConfig("SOPIP", strIP));
ASSERT(theApp.GetConfig("SOPPORT", strPort));
ASSERT(theApp.GetConfig("TIMEOUT", strTimeOut)); DTS_STRUCT* pdts = (DTS_STRUCT*)pParam; CCommSocket commSocket;
if (!commSocket.Create ())
{
AfxMessageBox("创建套接字失败!");
return false;
}
if (!commSocket.Connect(strIP, atoi(strPort)))
{
AfxMessageBox("连接服务器失败!");
return false;
}
commSocket.SendDataToServer(pdts->pszData, pdts->pCommHeader, pdts->ppszDataRet, &pdts->bTerminate, StopCommFunc); MSG msg;
while (!pdts->bTerminate) //捕获并处理消息,以正确处理收到服务器数据返回消息,但仍然无法正确接收,为什么呢?
{
::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
::TranslateMessage(&msg);
Sleep(1);
}
pdts->commStatus = commSocket.GetCommState();
pdts->pEvent->SetEvent();
return 0L;
}
我的实现代码如下:
//初始化线程参数,略
...
//启动线程进行数据发送
AfxBeginThread(SendDataProc, &dts);
if (WAIT_OBJECT_0 == WaitForSingleObject(m_eventComm, nTimeOut))
{
if (dts.commStatus == COMM_SUCCEED)
{
return true; //通过检查
}
else
{
AfxMessageBox("用户名或密码不正确!");
continue;
}
}
else
{
dts.bTerminate = true;
AfxMessageBox("服务器处理超时!");
continue;
}以下是工作线程函数代码:
UINT SendDataProc(LPVOID pParam)
{
CString strIP, strPort, strTimeOut;
ASSERT(theApp.GetConfig("SOPIP", strIP));
ASSERT(theApp.GetConfig("SOPPORT", strPort));
ASSERT(theApp.GetConfig("TIMEOUT", strTimeOut)); DTS_STRUCT* pdts = (DTS_STRUCT*)pParam; CCommSocket commSocket;
if (!commSocket.Create ())
{
AfxMessageBox("创建套接字失败!");
return false;
}
if (!commSocket.Connect(strIP, atoi(strPort)))
{
AfxMessageBox("连接服务器失败!");
return false;
}
commSocket.SendDataToServer(pdts->pszData, pdts->pCommHeader, pdts->ppszDataRet, &pdts->bTerminate, StopCommFunc); MSG msg;
while (!pdts->bTerminate) //捕获并处理消息,以正确处理收到服务器数据返回消息,但仍然无法正确接收,为什么呢?
{
::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
::TranslateMessage(&msg);
Sleep(1);
}
pdts->commStatus = commSocket.GetCommState();
pdts->pEvent->SetEvent();
return 0L;
}
解决方案 »
- asp 调用VC写的com组件,无法fopen打开文件失败
- error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject
- 请教:文档/视图结构中,如何实现多层继承类(已串行化)对象的存储和读取?
- 朗玛趋势科技(北京)有限公司招聘游戏开发人员
- 如何在程序代码中获得资源中的VERSION信息呢?
- 如何用shellexecute函数
- 请问 完成端口中 SEND_POSTED 这个数据是怎么来的?
- 大侠帮忙,控件重画和在控件上画图的问题
- 怎样用CMarkup遍历XML文件
- vc2008用ado连Sql Server2008的问题
- 各位大哥,我自己定义的一个类从Csocket派生出来的,但是编译的时候说我的socket类undefined,各位大哥,怎么解决啊??
- 如何动态隐藏/显示菜单项?
while (!pdts->bTerminate) {
if (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
Sleep(1);
}