C# Класс 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.
Показать файл Открыть проект

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
InternetGetConnectedState ( int &connDescription, int reservedValue ) : bool

Описание методов

Check() публичный статический Метод

public static Check ( Method method, string address, int port ) : bool
method Method
address string
port int
Результат bool

Check() публичный статический Метод

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
Результат bool

DnsTest() публичный статический Метод

public static DnsTest ( string url = DefaultHost ) : bool
url string
Результат bool

IsConnectionAvailable() публичный статический Метод

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
Результат bool

PingTest() публичный статический Метод

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
Результат bool

TcpSocketTest() публичный статический Метод

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
Результат bool

WebRequestTest() публичный статический Метод

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.
Результат bool