Detecting Active Internet Connections

Document ID: 100171
Products: All Products, All Platforms
Last Reviewed: April 16, 2002


There is no single best method that will determine if you have an active connection to the Internet. In many cases, the appropriate solution is to simply make the connection attempt and handle any resulting errors such as if there is a timeout or the connection is refused. However, there may be cases where your application needs to determine this independently of any specific application protocol. This article discusses several approaches that can be used, usually in combination with one another.

More Information

The WinInet API
At the highest level, Microsoft's WinInet API has several functions which report information about the configuration of the system and whether or not it may be connected to the Internet. The InternetGetConnected state function returns a boolean value, along with various status flags. While this offers the simplest approach, what this function really tells you is if the system will prompt you to establish a dial-up networking connection by checking the system configuration. In other words, if you have a default dial-up connectoid defined, then this function checks to see if it is active. If you are configured to connect through a LAN, then it will check to make sure a default gateway IP address has been specified. Because it uses the system's configuration information in the registry, it is possible for it to return false (not connected) even though the system does have an active connection to the Internet. There are also functions that are part of the Synchronization Manager API called IsDestinationReachable and IsNetworkAlive. These are only available in later versions of Windows, or systems with Internet Explorer 5 or later installed, and returns information about the system such as if a network card is installed or a dial-up networking connection is active.

The Remote Access Services API
This is a lower-level interface that is used to establish dial-up networking connections to Internet service providers. If the user has one or more dial-up networking connections configured, the API can be used to determine if a connection is active or not. Both SocketTools and SocketWrench include an ActiveX control and C++ class for this API which can be used in your applications to determine if a dial-up networking connection is active or not. First, determine if there are any dial-up connections configured on the local system. If there aren't any, then the system either does not have an Internet connection, or it is connected through a LAN. If there is at least one connection, check to see what protocols it it configured to use; with the control, you would check the NetworkProtocol property. If it is configured to use the TCP/IP protocol, then check to see if that connection is active. If you find an active connection enumerating through the available connections, then it is likely that the user is connected to the Internet. Note, however, it is possible that they may be connected to a corporate WAN, and not the Internet itself. An active dial-up connection in of itself doesn't guarantee an Internet connection, but is a strong indicator of one.

Internet Control Message Protocol
ICMP is a protocol that works at a lower level than the TCP or UDP protocols, and one of its functions is the ability to send echo datagrams to a remote host. If that host is available, it then sends a response back to the system that originated the echo. This is called "pinging" a remote host, and SocketTools includes a component that allows an application to do this easily. By sending an ICMP echo datagram to a well-known host on the Internet and checking for a response, you can determine if the system has an active Internet connection. Although generally reliable, this approach can have several drawbacks. Many systems have routers configured to block ICMP datagrams in an effort to prevent a variety of Denial of Service (DoS) attacks. There also may be situations where a datagram is not received, but the remote host is actually available; for example, an intermediate router may fail. In both cases, not receiving a response does not automatically mean there is no active Internet connection. To account for this, an application may choose to send a sequence of pings to different hosts, and conclude that there is no connection if none of those pings are responded to. However, this can be a sensitive issue since your application may be construed as "flooding" servers with ICMP echo datagrams and be mistaken as an attack against the system. Generally speaking, this type of approach is only acceptable when the well-known server is one that you own or have explicit permission to access.

In summary, there is no foolproof way to determine if the system has an active Internet connection. Even using a combination of the methods discussed above could produce a false negative as the result of a transitory error or nonstandard system configuration and this should be accounted for in any application.


Microsoft Knowledge Base articles Q242558 and Q315035