如何测试网络连通性
2011-01-25 10:06
337 查看
Introduction
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. In this article, I will try to demonstrate a couple of ways to overcome this problem.
Method 1: WebRequest
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.
Method 2: TCP Socket
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.
Method 3: Ping
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 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:
However, that will require DNS lookup which causes extra delay.
Method 4: DNS Lookup
Alternatively you can use DNS lookup to check internet connectivity. This method is faster than Ping method.
Method 5: Windows Internet API (WinINet)
WinINet API provides functions to test internet connectivity, such as InternetCheckConnection and InternetGetConnectedState. I do not have any idea about what these fucntions do exactly to test internet connectivity. You may refer to http://msdn.microsoft.com/en-us/library/aa384346(v=VS.85).aspx and http://msdn.microsoft.com/en-us/library/aa384702(v=VS.85).aspx for details.
An example usage can be:
Summary
In this article, we have seen a couple of different ways to test internet connectivity. Each method has its own advantages and disadvantages. So, it is up to you to choose the best way for your platform and application.
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. In this article, I will try to demonstrate a couple of ways to overcome this problem.
Method 1: WebRequest
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 bool WebRequestTest(){ string url = "http://www.google.com"; try{ System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url); System.Net.WebResponse myResponse = myRequest.GetResponse(); }catch (System.Net.WebException){ return false; } return true; }
Method 2: TCP Socket
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 bool TcpSocketTest(){ try{ System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("www.google.com", 80); client.Close(); return true; }catch (System.Exception ex){ return false; } }
Method 3: Ping
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 bool PingTest(){ System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"),1000); if (pingStatus.Status == System.Net.NetworkInformation.IPStatus.Success){ return true; }else{ return false; } }
You cannot use this method in .NET Compact Framework because there is no 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.
Method 4: DNS Lookup
Alternatively you can use DNS lookup to check internet connectivity. This method is faster than Ping method.
public static bool DnsTest(){ try{ System.Net.IPHostEntry ipHe = System.Net.Dns.GetHostByName("www.google.com"); return true; }catch{ return false; } }
Method 5: Windows Internet API (WinINet)
WinINet API provides functions to test internet connectivity, such as InternetCheckConnection and InternetGetConnectedState. I do not have any idea about what these fucntions do exactly to test internet connectivity. You may refer to http://msdn.microsoft.com/en-us/library/aa384346(v=VS.85).aspx and http://msdn.microsoft.com/en-us/library/aa384702(v=VS.85).aspx for details.
An example usage can be:
[DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int connDescription, int ReservedValue); //check if a connection to the Internet can be established public static bool IsConnectionAvailable() { int Desc; return InternetGetConnectedState(out connDesc, 0); }
Summary
In this article, we have seen a couple of different ways to test internet connectivity. Each method has its own advantages and disadvantages. So, it is up to you to choose the best way for your platform and application.
相关文章推荐
- 如何获取本地网络中的所有(sqlserver)服务器名称,并测试其连通性!
- 如何测试网络连通性
- java如何测试网络连通性
- 如何测试网络连通性
- 如何测试Azure虚拟机网络连通性
- 使用Java测试网络连通性的几种方法
- 测试ipv6网络连通性的UDP协议测试代码
- 如何在低速率网络中测试 Web 应用
- 如何确保VDI网络的连通性及安全性
- 如何在低速率网络中测试 Web 应用
- 网络连通性测试
- 路由与网络连通性(三)路由测试及INTERNET的连接
- 网络游戏性能测试一步曲:如何规划游戏产品性能测试(草稿)
- 使用Java测试网络连通性的几种方法
- linux ping命令参数及用法详解--linux测试网络连通性命令
- 使用Java测试网络连通性的几种方法
- 使用Java测试网络连通性的几种方法
- ping命令使用介绍及怎样使用Ping命令来测试网络连通性
- 多网卡时如何使用源地址ping目的地址,检测网络连通性
- RHCE 系列(一):如何设置和测试静态网络路由