C# Class Common.ConnectStatus

Some of the functions of our applications may require a run-time test of internet connectivity. Once internet connectivity is detected, the functions that require internet access may temporarily be disabled and/or the user can be notified via an alert message. Otherwise, the application may result in error during operation or it may cause annoying problems for the user.
Show file Open project: Revelations/BaconApp

Public Methods

Method Description
Check ( Method method, string address, int port ) : bool
Check ( Method method = DefaultMethod, string host = DefaultHost, string ipv4 = DefaultIpAddress, int port = DefaultPort, int timeout = DefaultTimeout ) : bool

Checks whether there is an internet connection. Defaults to calling the IsConnectionAvailable method.

DnsTest ( string url = DefaultHost ) : bool

IsConnectionAvailable ( ) : bool

Check if a connection to the Internet can be established. Much faster than the other methods, as it does not require a DNS lookup.

PingTest ( string address = DefaultIpAddress, int timeout = DefaultTimeout ) : bool

Ping an IP address.

There can be some delay in response of web request, therefore this method may not be fast enough for some applications. A better way is to check whether port 80, default port for http traffic, of an always online website. You cannot use this method in .NET Compact Framework because there is no System.Net.NetworkInformation namespace that comes with Compact Framework. However, you can use Smart Device Framework ("http://www.opennetcf.com", Community Edition is free for download) provided by OpenNETCF. This framework comes with many other useful tools that .NET Compact Framework does not contain. Notice that I used Google’s IP address 208.69.34.231. We could use Google’s web address www.google.com: System.Net.NetworkInformation.PingReply pingStatus = ping.Send("www.google.com",1000); However, that will require DNS lookup which causes extra delay.

TcpSocketTest ( string address = DefaultHost, int port = DefaultPort ) : bool

There can be some delay in response of web request therefore this method may not be fast enough for some applications. A better way is to check whether port 80, default port for HTTP traffic, of an always online website.

WebRequestTest ( string address = DefaultHost ) : bool

We may send a web request to a website which assumed to be online always, for example google.com. If we can get a response, then obviously the device that runs our application is connected to the internet.

Private Methods

Method Description
InternetGetConnectedState ( int &connDescription, int reservedValue ) : bool

Method Details

Check() public static method

public static Check ( Method method, string address, int port ) : bool
method Method
address string
port int
return bool

Check() public static method

Checks whether there is an internet connection. Defaults to calling the IsConnectionAvailable method.
public static Check ( Method method = DefaultMethod, string host = DefaultHost, string ipv4 = DefaultIpAddress, int port = DefaultPort, int timeout = DefaultTimeout ) : bool
method Method
host string
ipv4 string
port int
timeout int
return bool

DnsTest() public static method

public static DnsTest ( string url = DefaultHost ) : bool
url string
return bool

IsConnectionAvailable() public static method

Check if a connection to the Internet can be established. Much faster than the other methods, as it does not require a DNS lookup.
public static IsConnectionAvailable ( ) : bool
return bool

PingTest() public static method

Ping an IP address.
There can be some delay in response of web request, therefore this method may not be fast enough for some applications. A better way is to check whether port 80, default port for http traffic, of an always online website. You cannot use this method in .NET Compact Framework because there is no System.Net.NetworkInformation namespace that comes with Compact Framework. However, you can use Smart Device Framework ("http://www.opennetcf.com", Community Edition is free for download) provided by OpenNETCF. This framework comes with many other useful tools that .NET Compact Framework does not contain. Notice that I used Google’s IP address 208.69.34.231. We could use Google’s web address www.google.com: System.Net.NetworkInformation.PingReply pingStatus = ping.Send("www.google.com",1000); However, that will require DNS lookup which causes extra delay.
public static PingTest ( string address = DefaultIpAddress, int timeout = DefaultTimeout ) : bool
address string
timeout int
return bool

TcpSocketTest() public static method

There can be some delay in response of web request therefore this method may not be fast enough for some applications. A better way is to check whether port 80, default port for HTTP traffic, of an always online website.
public static TcpSocketTest ( string address = DefaultHost, int port = DefaultPort ) : bool
address string The address to try to connect to. Defaults to .
port int The port to use. Defaults to
return bool

WebRequestTest() public static method

We may send a web request to a website which assumed to be online always, for example google.com. If we can get a response, then obviously the device that runs our application is connected to the internet.
public static WebRequestTest ( string address = DefaultHost ) : bool
address string The address to try to connect to. Defaults to Google.
return bool