如何测试网络连通性

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值