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.
  • 127.0.0.1 - 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: http://lancanyon.com/autonet.htm for detail);
  • Have you reason for other ranges exclusion?
Please note that I do not exclude other specific IP adresses such as 0.0.0.0 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;
   str="";
   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(j==0)
    {
     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 != "127.0.0.1" 
   && !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;
}

Downloads

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


Comments

  • 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?

    Reply
  • 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 192.168.0.100 - 192.168.0.115. 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.

    Nizchka
    nizchka@hotmail.com

    Reply
  • 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???

    Reply
  • 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...

    Reply
  • 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?

    [code]
    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;
    }
    [/code]

    Reply
  • Bytes! To and From Internet?

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

    Originally posted by: Onkar Singh

    Hi,
    
    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?

    Regards,
    Onkar Singh

    http://developers.faithweb.com

    Reply
  • undefine variable ??bProxyConnection

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

    Originally posted by: kafka

    hi

    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;
    }

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds