用InternetCheckConnection 判断是否接通外网

InternetCheckConnection 通过尝试连接特定的网址,可以用来判断目前机器是否接通外网。例如:

BOOL bConnected = InternetCheckConnection(_T("http://www.google.com"), FLAG_ICC_FORCE_CONNECTION, 0);

使用时第二个参数一定要设置为FLAG_ICC_FORCE_CONNECTION,这样才会尝试去建立连接。当目标地址不可用时,这个函数返回会比较慢,注意不要在UI线程中使用。

这个函数会解析出URL中的host地址,然后建立socket连接去ping这个地址。

InternetCheckConnection

Allows an application to check if a connection to the Internet can be established.

BOOL InternetCheckConnection(
  LPCTSTR lpszUrl,
  DWORD dwFlags,
  DWORD dwReserved
);

Parameters

lpszUrl
[in] Pointer to a null-terminated string that specifies the URL to use to check the connection. This value can be NULL.
dwFlags
[in] Options. FLAG_ICC_FORCE_CONNECTION is the only flag that is currently available. If this flag is set, it forces a connection. A sockets connection is attempted in the following order:
  • If lpszUrl is non-NULL, the host value is extracted from it and used to ping that specific host.
  • If lpszUrl is NULL and there is an entry in the internal server database for the nearest server, the host value is extracted from the entry and used to ping that server.
dwReserved
[in] Reserved. Must be zero.

Return Values

Returns TRUE if a connection is made successfully, or FALSE otherwise. Use GetLastError to retrieve the error code. ERROR_NOT_CONNECTED is returned by GetLastError if a connection cannot be made or if the sockets database is unconditionally offline.

你可能感兴趣的:(server,socket,application,database,internet,Sockets)