C#检查互联网连接
你能告诉我,当我的C#程序运行时,有没有办法检查我的电脑是否连接了互联网。举个简单的例子,如果internet正在工作,我会输出一个消息框,说C#检查互联网连接,c#,networking,C#,Networking,你能告诉我,当我的C#程序运行时,有没有办法检查我的电脑是否连接了互联网。举个简单的例子,如果internet正在工作,我会输出一个消息框,说internet可用。否则,我会输出一条消息说,互联网不可用 不使用库函数查看网络是否可用(因为这不会检查internet连接) 或者不打开网页查看是否返回数据 using (WebClient client = new WebClient()) htmlCode = client.DownloadString("http://google.c
internet可用
。否则,我会输出一条消息说,互联网不可用
不使用库函数查看网络是否可用(因为这不会检查internet连接)
或者不打开网页查看是否返回数据
using (WebClient client = new WebClient())
htmlCode = client.DownloadString("http://google.com");
因为以上两种方法都不适合我的需要。请考虑以下代码片段
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success)
{
// presumably online
}
祝你好运 稍微短一点的版本:
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}
另一个选择是:
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success) {
// presumably online
}
您可以找到更广泛的讨论刚刚编写了异步函数来实现这一点:
private void myPingCompletedCallback(object sender, PingCompletedEventArgs e)
{
if (e.Cancelled)
return;
if (e.Error != null)
return;
if (e.Reply.Status == IPStatus.Success)
{
//ok connected to internet, do something...
}
}
private void checkInternet()
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(myPingCompletedCallback);
try
{
myPing.SendAsync("google.com", 3000 /*3 secs timeout*/, new byte[32], new PingOptions(64, true));
}
catch
{
}
}
我的NetworkMonitor类现在提供了以下内容(基于此处的其他响应): 这是我的方法
public static bool ConnectToInternet(int timeout_per_host_millis = 1000, string[] hosts_to_ping = null)
{
bool network_available = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
if (network_available)
{
string[] hosts = hosts_to_ping ?? new string[] { "www.google.com", "www.facebook.com" };
Ping p = new Ping();
foreach (string host in hosts)
{
try
{
PingReply r = p.Send(host, timeout_per_host_millis);
if (r.Status == IPStatus.Success)
return true;
}
catch { }
}
}
return false;
}
pinging呢?它比打开一个页面消耗更少。+1与其他响应不同,特别是在try块中包装时。Ping.Send()可能引发许多异常,其中可能包括DNS查找失败。与此处的其他响应不同,需要将其包装在try块中。Ping.Send()可以抛出许多异常,其中可能包括DNS查找失败。这实际上会导致误报;计算机的DNS服务器可能已经缓存了“www.google.com”,但没有互联网连接。或计算机的DNS客户端服务可能也缓存了它。获取目标的IP是一回事,连接到目标是另一回事!使用“google.com”需要更多的时间,因为它需要解决。相反,直接使用IP进行ping将更快。ping到Google公共DNS IP地址(
8.8.8.8
或8.8.4.4
)对我来说很好。尽管你的代码在中国不起作用。我正在结合这两种选择。
public bool IsInternetAvailable
{
get { return IsNetworkAvailable && _CanPingGoogle(); }
}
private static bool _CanPingGoogle()
{
const int timeout = 1000;
const string host = "google.com";
var ping = new Ping();
var buffer = new byte[32];
var pingOptions = new PingOptions();
try {
var reply = ping.Send(host, timeout, buffer, pingOptions);
return (reply != null && reply.Status == IPStatus.Success);
}
catch (Exception) {
return false;
}
}
public static bool ConnectToInternet(int timeout_per_host_millis = 1000, string[] hosts_to_ping = null)
{
bool network_available = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
if (network_available)
{
string[] hosts = hosts_to_ping ?? new string[] { "www.google.com", "www.facebook.com" };
Ping p = new Ping();
foreach (string host in hosts)
{
try
{
PingReply r = p.Send(host, timeout_per_host_millis);
if (r.Status == IPStatus.Success)
return true;
}
catch { }
}
}
return false;
}
public static bool HasConnection()
{
try
{
System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("www.google.com");
return true;
}
catch
{
return false;
}
}