|
开发一个网络程序,需要兼容cmnet和cmwap,因为事先不知道网络类型,所以采用先去联接10.0.0.172 80端口,如果成功就判断是cmwap,然后再联www.baidu.com,如果成功则是cmnet,但问题是:
1、不管用CSocket还是CCeSocket,程序都偶尔会死在::Connect(host,port)上。
2、用CSock类继承CCeSocket,重载ConnectHelper后,将socket句柄变为非阻塞,然后再循环检查,连接状态,也总是会连接
不上。但通过wireshark对模拟器抓包发现有google的响应包。
另外在ppc2003模拟器上用IE可以打开www.baidu.com
附上代码,请高手们看看:
BOOL CUtility::IsWAPNet()
{
CSock wsWAPServer;
g_mylog.WriteLog(_T("IsWAPNet::Create"),LNORMAL);
wsWAPServer.SetTimeOut(5);
if(!wsWAPServer.Create())
{
return FALSE;
}
CString wapserver=CMWAP_SERVER ;
g_mylog.WriteLog(_T("IsWAPNet::Connect"),LNORMAL);
if(!wsWAPServer.Connect(wapserver,80))
{
g_mylog.WriteLog(_T("IsWAPNet::Connect fail"),LNORMAL);
return FALSE;
}
g_mylog.WriteLog(_T("IsWAPNet::Send"),LNORMAL);
if(wsWAPServer.Send("\0",1,0)<=0)
{
g_mylog.WriteLog(_T("IsWAPNet::Send fail"),LNORMAL);
return FALSE;
}
return TRUE;
}
BOOL CUtility::IsNetGood()
{
if(IsWAPNet())
{
return TRUE;
}
g_mylog.WriteLog(_T("IsCMNet!"),LNORMAL);
CSock wsSMTPServer;
if(!wsSMTPServer.Create())
{
return FALSE;
}
wsSMTPServer.SetTimeOut(25);
CString wapserver="www.baidu.com" ;
if(!wsSMTPServer.Connect(wapserver,80))
{
g_mylog.WriteLog(_T("IsCMNet::Connect fail"),LNORMAL);
return FALSE;
}
g_mylog.WriteLog(_T("IsCMNet::Send"),LNORMAL);
if(wsSMTPServer.Send("\0",1,0)<=0)
{
g_mylog.WriteLog(_T("IsCMNet::Send fail"),LNORMAL);
return FALSE;
}
return TRUE;
}
BOOL CSock::ConnectHelper(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
g_mylog.WriteLog(_T("CSock::ConnectHelper"),LNORMAL);
if (m_pbBlocking != NULL)
{
WSASetLastError(WSAEINPROGRESS);
return FALSE;
}
m_nConnectError = -1;
unsigned long argp = 1;
int ret = ioctlsocket(m_hSocket,FIONBIO,(unsigned long*)&argp);
if(ret == SOCKET_ERROR)
{
closesocket(m_hSocket);
g_mylog.WriteLog(_T("CAsyncSocket::SET_SOCK_OPT_FAIL"),LNORMAL);
return FALSE; //SET_SOCK_OPT_FAIL
}
g_mylog.WriteLog(_T("CAsyncSocket::ConnectHelper"),LNORMAL);
if (!CCeSocket::ConnectHelper(lpSockAddr, nSockAddrLen))
{
if (GetLastError() == WSAEWOULDBLOCK)
{
CTime curt, st;
CTimeSpan span(0, 0, 0, m_dwTimeOut);
st = CTime().GetCurrentTime();
g_mylog.WriteLog(_T("CAsyncSocket::WSAEWOULDBLOCK"),LNORMAL);
while (PumpMessages(FD_CONNECT))
{
curt = CTime().GetCurrentTime();
if(curt > (st+span))
{
g_mylog.WriteLog(_T("CSock::time out"),LNORMAL);
return FALSE; // No need for idle time processing.
}
g_mylog.WriteLog(L"no timeout",LNORMAL);
if (m_nConnectError != -1)
{
WSASetLastError(m_nConnectError);
return (m_nConnectError == 0);
}
}
}
return FALSE;
}
return TRUE;
}
|
|