C# 测试活动的互联网连接.ping google.com [英] C# Testing active internet connection. Pinging google.com

查看:55
本文介绍了C# 测试活动的互联网连接.ping google.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 2008

我正在使用此代码来测试互联网连接.因为我的应用程序必须登录到 Web 服务器.但是,如果用户互联网连接失败或电缆拔出.我将不得不通知用户.

I am using this code to test for an internet connection. As my application will have to login to a web server. However, if the user internet connection was to fail or cable pulled out. I will have to notify the user.

 // Ping www.google.com to check if the user has a internet connection.
    public bool PingTest()
    {
        Ping ping = new Ping();

        PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));

        if (pingStatus.Status == IPStatus.Success)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

我认为可以测试 Internet 连接的唯一方法是 ping www.google.com.所以我使用了一个服务器计时器,我将它设置为 500 毫秒,并且在失效事件中它会调用这个 ping 函数.如果 ping 返回 false.然后我的应用会采取适当的行动.

The only way I think I can test for an Internet connection is to ping www.google.com. So I have a used a server timer which I have set for 500 milliseconds and in the lapsed event it will call this ping function. If the ping returns false. Then my app will take appropriate action.

您认为使用谷歌作为测试互联网连接的方式是一件好事吗?如果谷歌失败,那么我的应用程序将无法运行.轮询 1/2 秒是多还是少?只是想知道我的整个想法是否好?

Do you think using google as a way to test an Internet connect is a good thing. If google was to fail, then my app would not function. Is polling 1/2 second to much or too little? Just wondering about my whole idea if it is good or not?

非常感谢,

推荐答案

为什么要 ping Google?您真正关心的唯一服务器是您要与之通信的 Web 服务器,对吗?所以改为ping那个.这样,您可以在用户与网络服务器之间出现任何问题时通知用户 - 这是唯一重要的事情.

Why ping Google? The only server you really care about is the web server you want to talk to, right? So ping that instead. That way, you can notify the user if anything is wrong between them and the web server - that's the only thing that matters.

如果互联网出现问题,这意味着用户只能在自己的城镇内做任何事情,您可能无法访问 Google,但如果您仍然可以访问您感兴趣的网络服务器,为什么要报告问题?另一方面,如果网络服务器出现故障而 Google 没有出现故障,您为什么想告诉用户存在问题?

If something goes wrong with the internet which means that the user can only do anything within their own town, you may not be able to reach Google but if you can still reach the web server you're interested in, why report a problem? On the other hand, if the web server goes down but Google doesn't, why would you not want to tell the user that there's a problem?

同样,为什么要使用 ping?为什么不向 Web 服务器上的已知 URL 发送 HTTP 请求?毕竟,如果网络已启动但 Web 服务器进程本身已关闭,那肯定与用户的网络出现故障一样糟糕.

Likewise, why use a ping? Why not send an HTTP request to a known URL on the web server? After all, if the network is up but the web server process itself is down, surely that's just as bad as the user's network going down.

我在问题中没有注意到您每半秒重复一次.这对任何人来说都不是很愉快.肯定一分钟一次就足够了,不是吗?

I hadn't noticed in the question that you were repeating this every half second. That's really not pleasant for anyone. Surely once a minute is often enough, isn't it?

这篇关于C# 测试活动的互联网连接.ping google.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆