搞了一下午,什么InternetCheckConnection,InternetConnect,InternetQueryOption试了一堆,发现这些函数要不没用,要不用起来很麻烦,根本检测不了网络的状态。因为机子上本来有WIFI和本地网络,所以就用SOCKET了 这是朋友给的一个函数,方便以后使用就放这里了
BOOL CheckNet()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
{
return FALSE;
}
SOCKET sckClient;
SOCKADDR_IN m_sockaddr;
memset( &m_sockaddr, 0, sizeof( m_sockaddr ) );
if ( (sckClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET )
{
return FALSE;
}
hostent *hostEnt = NULL;
long lIPAddress = 0;
hostEnt = gethostbyname("www.google.cn");
if( hostEnt != NULL )
{
lIPAddress = ((in_addr*)hostEnt->h_addr)->s_addr;
m_sockaddr.sin_addr.s_addr = lIPAddress;
}
else
{
m_sockaddr.sin_addr.s_addr = inet_addr("www.google.cn");
}
m_sockaddr.sin_family = AF_INET;
m_sockaddr.sin_port = htons( 80 );
if( connect( sckClient, (SOCKADDR*)&m_sockaddr, sizeof( m_sockaddr ) ) == SOCKET_ERROR )
{
closesocket(sckClient);
return FALSE;
}
closesocket(sckClient);
return TRUE;
}