Function to Verify if Connected to the Internet

In addition, I'd like to add that this function is based on the comments made on this site by Jarek Gibek as well as the article 'Get hostname and ip address of local computer(2)' by Jaroslav Pisk.

How it Works

The way this function works is that it attempts to define which are the IP addresses which can automatically be excluded from consideration. Then it defines which IP addresses would signify that the computer is connected to the Internet. Here's a sampling of IP addresses and what they mean to the function that should help to illustrate my point.
  • - localhost;
  • Private Adress area(RFC 1597)
    • 10.*.*.* - Class A;
    • 172.16-31.*.* - Class B;
    • 192.168.*.*- Class C;
  • 169.254.*.* Microsoft AutoNet address area(See: for detail);
  • Have you reason for other ranges exclusion?
Please note that I do not exclude other specific IP adresses such as or 255.2555.255.255 because the local host can't have either of these IP addresses.

The Function

While the function is stated (and commented) here, you can also download it below.
#include "winsock2.h"

BOOL IsInternetConnection()
 char szHostName[128];

 BOOL bPrivateAdr = false; // Private Address area
 BOOL bClassA = false;     // Class A definition
 BOOL bClassB = false;     // Class B definition
 BOOL bClassC = false;     // Class C definition
 BOOL bAutoNet = false;    // AutoNet definition
 CString str;

 if (gethostname(szHostName, 128) == 0 )
  // Get host adresses
  struct hostent * pHost;
  int i;
  UINT ipb;
  pHost = gethostbyname(szHostName); 

  for (i = 0; 
       pHost!= NULL && pHost->h_addr_list[i]!= NULL; 
       i++ )
   int j;
   bClassA = bClassB = bClassC = false;
   for( j = 0; j < pHost->h_length; j++ )
    CString addr;

    if( j > 0 )	str += ".";
    ipb = (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j];

    // Define the IP range for exclusion
     if(bClassA = (ipb == 10)) break; // Class A defined
     bClassB = (ipb == 172); 
     bClassC = (ipb == 192);
     bAutoNet = (ipb == 169);
    else if (j==1)
     // Class B defined
     if(bClassB = (bClassB && ipb >= 16 && ipb <= 31)) break;

     // Class C defined
     if(bClassC = (bClassC && ipb == 168)) break;

     //AutoNet pasibility defined
     if(bAutoNet = (bAutoNet && ipb == 254)) break;

    addr.Format("%u", ipb );
    str += addr;
   // If any address of Private Address 
   // area has been found bPrivateAdr = true
   bPrivateAdr = bPrivateAdr || bClassA || bClassB || bClassC;

   // If any address of Internet Address area 
   // has been found returns TRUE
   if (!bClassA 
   && !bClassB 
   && !bClassC 
   && !bAutoNet 
   && str != "" 
   && !str.IsEmpty()) 
    return TRUE;

 if (bPrivateAdr)
  // The system has IP address from Private Address 
  // area,only. Internet from the computer can be accessable 
  // via Proxy. If user turn on proxy connection flag, the 
  // function believe Internet accessable.
  return bProxyConnection;
 return false;


Download source (same function as listed above) - 1 Kb Date Posted: May 15


  • Seems doesn't work on Win98

    Posted by Legacy on 01/08/2003 12:00am

    Originally posted by: max

    On Win98 OS, in case of network card function gethostbyname continues to return the machine IP while disconnected(unplugged) Anybody knows solution for Win98?

  • Behind firewall/router

    Posted by Legacy on 01/03/2003 12:00am

    Originally posted by: nizchka

    shouldn't 'char szHostName[128];' (actually 127 chars) be
    'char szHostName[128 + 1];' because the functioncall which fills szHostName ('if (gethostname(szHostName, 128) == 0 )') gets a maxbufsize of 128 chars. Anyway I think that the error situation never happens because I don't know of such long hostnames.

    But that is not my main question. A lot of Inernet users connect to the internet through a firewall/router. My router gives the local network users a ip addr in the range - This IP range 192.168.x.x is dedicated for your private internal net. I routed through my firewall/router, and am 24 hours a day on the net. This situation happens a lot. In your code this is given as not connected.

    Anyway thanks for your work on this. Maybe there is a better way to check if youre connected to the internet.


  • input an ip address

    Posted by Legacy on 09/28/2002 12:00am

    Originally posted by: Mark Wall

    is there any way that i can type an ip address, then it takes the first part of the ip address and converts it to bin then does the check and then out puts what class it is in. i tried to compile on unix using g++ but it does not recongize winsock2.h, even when i copy the file to the same dir. and ideas???

  • bProxyConnection explained

    Posted by Legacy on 08/02/2001 12:00am

    Originally posted by: jb

    bProxyConnection is meant to be an external variable that the user can change if they are on connected to a proxy network. so in your UI somewhere, let the user set bProxyConnection to true if they are on a proxy. otherwise this function will return false even though the user may be connected through a proxy. note if bProxyConnection is true, it still doesn't mean they are connected to the internet, just that they MAY be connnected...

  • Regarding bProxyConnection

    Posted by Legacy on 04/20/2001 12:00am

    Originally posted by: RCFox

    Am I correct in that the author is saying that if
    bPrivateAdr equals true, then a proxy connection to the Internet may exist, and an applicaiton should use something such as Connect() to check for a connection in this case?

    if (bPrivateAdr)
    // The system has IP address from Private Address
    // area,only. Internet from the computer can be accessable
    // via Proxy. If user turn on proxy connection flag, the
    // function believe Internet accessable.
    return bProxyConnection;

  • Bytes! To and From Internet?

    Posted by Legacy on 04/11/2001 12:00am

    Originally posted by: Onkar Singh

    I want to know if there is a way to know how much internet data transfer ( in bytes ) took place between a certain time interval?

    Onkar Singh

  • undefine variable ??bProxyConnection

    Posted by Legacy on 03/24/2001 12:00am

    Originally posted by: kafka


    anyone know what will bProxyConnection become ? seem like an undefine variable ?

    if (bPrivateAdr)
    // The system has IP address from Private Address
    // area,only. Internet from the computer can be accessable
    // via Proxy. If user turn on proxy connection flag, the
    // function believe Internet accessable.
    return bProxyConnection;

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date