Monitor Internet Connection State


Introduction: 

This article describes an easy approach to building two controls used to monitor the status of an internet connection and provide the user with some indication of that status. Within the attached project, there are two controls, one shows the user what the connection type is and whether or not the machine is connected or offline, the other one is used to show some indication of the quality of the connection in terms of whether or not the connection is good, intermittent, or offline.

The purpose of the controls was to provide a mobile user of a smart client application some status information regarding the internet connection; in this instance, there were many forms within the application and I wanted a simple control to drop on each form to provide that status.

 ConnectionState1.gif

Figure 1:  Both Connection Status Controls In Use

Getting Started:

In order to get started, unzip the attachment and load the solution into Visual Studio 2005. Examine the solution explorer and note the files contained each of the two projects:

ConnectionState2.gif

Figure 2:  The Solution Explorer Showing the Project Files

The "ConnectStatus" project contains the two controls used to display internet connection status to the user; the "ConnectQualityView" displays an indication of the quality of the status based upon how well that connection is maintained over time. The "ConnectStateView" control shows the type of connection and provides a graphic indicating whether or not the connection is active or offline.

The second project, "TestAppForInetConnect" contains a simple form used to display both controls at the same time. This second project is not necessary as with Visual Studio 2005, the user may display the controls in the control test container.  There is no code associated with this second project, the main form has one of each type of control loaded into it and it serves only as a container for those controls.

The Code:  ConnectStateView.

The control is quite simple; the visual elements include only a group box and a single label. Aside from the visual elements, there is a single timer which is used to check the status of the internet connection repeatedly and there is a single image list which is used to hold a couple of images used to place an icon adjacent to the label control.

If you care to open the class, you will note that there are no imports. The control's code is pretty easy to read and is as follows:

public partial class ConnectStateView

{                

    #region "Declarations"

                  

    private string ConnectionStateString;                  

    [DllImport("wininet.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]

    private static extern bool InternetGetConnectedState(ref int lpSFlags, int dwReserved);

                  

    public enum InetConnState

    {

        modem = 0x1,

        lan = 0x2,

        proxy = 0x4,

        ras = 0x10,

        offline = 0x20,

        configured = 0x40

    }

                  

    #endregion                  

                  

    #region "Control Methods"

                  

    private void ConnectStateView_Load(object sender, System.EventArgs e)

    {                            

        Timer1.Enabled = true;                            

    }

               

    private void Timer1_Tick(System.Object sender, System.EventArgs e)

    {                            

        bool blnState;

        blnState = CheckInetConnection();

        lblConnectStatus.Text = "Connection Type:  " + ConnectionStateString;                            

    } 

 

    public bool CheckInetConnection()

    {

        long lngFlags;

        if (InternetGetConnectedState(lngFlags, 0))

        {

            if (lngFlags & InetConnState.lan)

            {

                ConnectionStateString = "LAN.";

                lblConnectStatus.Image = ImageList1.Images(1);

            }

            else if (lngFlags & InetConnState.modem)

            {

                ConnectionStateString = "Modem.";

                lblConnectStatus.Image = ImageList1.Images(1);

            }

            else if (lngFlags & InetConnState.configured)

            {

                ConnectionStateString = "Configured.";

                lblConnectStatus.Image = ImageList1.Images(1);

            }

            else if (lngFlags & InetConnState.proxy)

            {

                ConnectionStateString = "Proxy";

                lblConnectStatus.Image = ImageList1.Images(1);

            }

            else if (lngFlags & InetConnState.ras)

            {

                ConnectionStateString = "RAS.";

                lblConnectStatus.Image = ImageList1.Images(1);

            }

            else if (lngFlags & InetConnState.offline)

            {

                ConnectionStateString = "Offline.";

                this.lblConnectStatus.Image = ImageList1.Images(2);

            }

        }

        else

        {

            ConnectionStateString = "Not Connected.";

            lblConnectStatus.Image = ImageList1.Images(3);

        }

    }                

    #endregion               

}

 

In the beginning of the code, note that there is a region called "Declarations" defined and within that region there are three declarations. The first declaration defines a string value that is used to keep track of the connection type used by the client's machine. 

The next declaration is the most important part of the code, it is the code that exposes a function from the wininet.dll to the application; that function is called "InternetGetConnectedState". This function accepts two arguments which in this case are two 32 bit integers; these could be longs but integers work fine here; the function returns a boolean but also could be set up to return a long integer as well.  In use, empty values of the specified data types are passed to this function and the function then sets their values. To use the control, these set values are evaluated in code to determine the type and status of the current internet connection.

The last declaration is of an enumeration used to contain representatives of each connection type exposed by the previous function. The flags identified in the enumeration are representative of each connection type that may be returned from making a call to the wininet.dll function declared previously.

After the declarations region is closed, a new region entitled "Control Methods" is defined. The first item in this section is the control load event handler; in this section, the timer is enabled and process of polling the internet connection of the timer's interval is initialized.

After the load event code, the handler for the timer is defined. The handler is used to evoke the "InternetGetConnectedState" function through a call to the "CheckInetConnect" method. This code calls the next method in line, "CheckInetConnection" which sets the text contained in the label based upon the value of the "ConnectionStateString" which is in turn updated by the "CheckInetConnection" method.

The "CheckInetConnection" method  calls the wininet.dll method "InternetGetConnectedState" which in turn sets the flag and connection type arguments used to determine the status of the internet connection. At the beginning of the evaluation, the first check determines whether or not the machine is connected; if the machine is connected, each pair is evaluated to determine the type of connection and to set the label text and icon to reflect the status returned by the method. If the connection is not active, the else block will execute and the label text and icon will update to show that the system is not connected (see bottom control in figure 3).

ConnectionState3.gif

Figure 3:  Connection Status Controls with Failed Internet Connection

That is all there is to the code for this first control. The next section will discuss the content of the second control as used to gauge the quality of an internet connection.

The Code:  ConnectQualityView.

This control is used to give a rough estimate as the quality of the internet connection as a function of the durability and persistence of the connection over a brief time span (3 seconds). The code contained within this class if nearly identical to the previous class; it is as follows:

public partial class ConnectQualityView

{

    #region "Declarations"

 

    string ConnectionQualityString = "Off"; 

    [DllImport("wininet.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]

    private static extern bool InternetGetConnectedState(ref int lpSFlags, int dwReserved);

 

    public enum InetConnState

    {

        modem = 0x1,

        lan = 0x2,

        proxy = 0x4,

        ras = 0x10,

        offline = 0x20,

        configured = 0x40

    }

 

    #endregion 

 

    #region "Control Methods" 

 

    private void ConnectQualityView_Load(object sender, System.EventArgs e)

    { 

        Timer1.Enabled = true;

        this.DoubleBuffered = true; 

    } 

 

    private void Timer1_Tick(System.Object sender, System.EventArgs e)

    { 

        lblConnectStatus.Refresh(); 

        bool blnState;

        blnState = CheckInetConnection(); 

    } 

 

    public bool CheckInetConnection()

    { 

        long lngFlags; 

        int temp_int =  (int) lngFlags;

        if (InternetGetConnectedState(ref temp_int, 0))

        {

            // True

            if (System.Convert.ToBoolean(lngFlags) && System.Convert.ToBoolean(InetConnState.lan))

            {

                switch (ConnectionQualityString)

                {

                    case "Good": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Intermittent": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Off": 

                        lblConnectStatus.ForeColor = Color.DarkOrange;

                        lblConnectStatus.Text = "Connection Quality:  Intermittent";

                        ConnectionQualityString = "Intermittent";

                        break;

                }

                this.Refresh();

            }

            else if (System.Convert.ToInt64(System.Convert.ToBoolean(lngFlags )) & (long)

                      InetConnState.modem)

            {

                switch (ConnectionQualityString)

                {

                    case "Good": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Intermittent": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Off": 

                        lblConnectStatus.ForeColor = Color.DarkOrange;

                        lblConnectStatus.Text = "Connection Quality:  Intermittent";

                        ConnectionQualityString = "Intermittent";

                        break;

                }

            }

            else if (System.Convert.ToInt64(System.Convert.ToBoolean(lngFlags )) & (long) 

                      InetConnState.configured)

            {

                switch (ConnectionQualityString)

                {

                    case "Good": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Intermittent": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Off": 

                        lblConnectStatus.ForeColor = Color.DarkOrange;

                        lblConnectStatus.Text = "Connection Quality:  Intermittent";

                        ConnectionQualityString = "Intermittent";

                        break;

                }

            }

            else if (System.Convert.ToInt64(System.Convert.ToBoolean(lngFlags )) & (long) InetConnState.proxy)

            {

                switch (ConnectionQualityString)

                {

                    case "Good": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Intermittent": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Off": 

                        lblConnectStatus.ForeColor = Color.DarkOrange;

                        lblConnectStatus.Text = "Connection Quality:  Intermittent";

                        ConnectionQualityString = "Intermittent";

                        break;

                }

            }

            else if (System.Convert.ToInt64(System.Convert.ToBoolean(lngFlags)) & (long) InetConnState.ras)

            {

                switch (ConnectionQualityString)

                {

                    case "Good": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Intermittent": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Off": 

                        lblConnectStatus.ForeColor = Color.DarkOrange;

                        lblConnectStatus.Text = "Connection Quality:  Intermittent";

                        ConnectionQualityString = "Intermittent";

                        break;

                }

            }

            else if (System.Convert.ToInt64(System.Convert.ToBoolean(lngFlags )) & (long) InetConnState.offline)

            {

                switch (ConnectionQualityString)

                {

                    case "Good": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Intermittent": 

                        lblConnectStatus.ForeColor = Color.Green;

                        lblConnectStatus.Text = "Connection Quality:  Good";

                        ConnectionQualityString = "Good";

                        break;

                    case "Off": 

                        lblConnectStatus.ForeColor = Color.DarkOrange;

                        lblConnectStatus.Text = "Connection Quality:  Intermittent";

                        ConnectionQualityString = "Intermittent";

                        break;

                }

            }

        }

        else

        {

            // False

            switch (ConnectionQualityString)

            {

                case "Good": 

                    lblConnectStatus.ForeColor = Color.DarkOrange;

                    lblConnectStatus.Text = "Connection Quality:  Intermittent";

                    ConnectionQualityString = "Intermittent";

                    break;

                case "Intermittent": 

                    lblConnectStatus.ForeColor = Color.Red;

                    lblConnectStatus.Text = "Connection Quality:  Off";

                    ConnectionQualityString = "Off";

                    break;

                case "Off": 

                    lblConnectStatus.ForeColor = Color.Red;

                    lblConnectStatus.Text = "Connection Quality:  Off";

                    ConnectionQualityString = "Off";

                    break;

            }

        } 

    } 

    #endregion 

}

 

In reviewing this code in contrast to the previous class described, you will see the only real difference is that the status is used to set the text and fore color of the label control used to display the status, and you will notice that the previous status message (gathered 1.5 seconds earlier) is evaluated to determine how to the display the status of the connection. This operates under a simple notion of promoting the status of the connection for remaining active over time. The control initializes with the connection quality string value set to "Off", when the connection state is evaluated, the initial connection state is noted and the code promotes the status to intermittent. If the status remains active for an additional 1.5 seconds, it is promoted to "Good". The idea here is that if the connection is dropping off and getting picked back up, the code will continually evaluate the current status against the previous status and promote or demote the status between the three available options of  Good, Intermittent, and Off.

Naturally you may alter the amount of time between status checks and in so doing, increase or decrease the amount of time necessary to promote or demote the status of the connection.

That is all there is to the second control.

Summary.

Whilst this example project demonstrates a couple of ways in which you can monitor and display status regarding a machine's internet connection, these approaches do not represent all of the ways that you may accomplish this task. Still and all, this approach is a simple and easy way to display connection status information to your users.

NOTE: THIS ARTICLE IS CONVERTED FROM VB.NET TO C# USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON VB.NET Heaven (http://www.vbdotnetheaven.com/). 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now