【问题标题】:How Can i Deal with Ping Exception in c#我如何处理 C# 中的 Ping 异常
【发布时间】:2020-03-24 08:21:25
【问题描述】:

我创建了一个简单的方法来检查我的 Internet 连接是否处于活动状态。不幸的是,由于 System.Net.Ping.dll 中的 System.Net.NetworkInformation.PingException”,try 部分被忽略了。值得注意的是,PingReply 出现了问题。 你能告诉我如何处理这个异常并允许代码正确运行以便执行 try 部分。

我的功能:

    private void ConnectionStatusChecker(object source, ElapsedEventArgs e)
    {

        Ping myPing = new Ping();
        String host = "https://www.google.com/";
        byte[] buffer = new byte[32];
        int timeout = 1000;
        PingOptions pingOptions = new PingOptions();

        Ping ping = new Ping();
        try
        {
            PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
            if (reply.Status == IPStatus.Success)
                ConnectionStatus = "Connected";
            else
                ConnectionStatus = "Disconnected";
        }
        catch (System.Net.NetworkInformation.PingException)
        {
            ConnectionStatus = "Disconnected-Exception";
        }
    }

编辑:我想在 ConnectionStatus 变量中存储有关我的 Internet 连接的信息。尝试部分描述了在断开或连接的情况下的值。但是 PingException 不允许我检查它 - 异常会自动阻止 try 部分。问题是,如何处理这个异常,我犯的根源或错误在哪里?

【问题讨论】:

  • 我不确定我是否理解这个问题。你能再澄清一点吗?
  • 我刚刚添加了一个小解释
  • 如果Ping.Send 失败并返回System.Net.NetworkInformation.PingException,将触发异常。这不是你想要的吗?
  • 但是如果我有连接并且谷歌服务器有效,为什么它会失败?
  • @Matheew 检查PingException 是否有InnerException。它可能会告诉你失败的原因。

标签: c# exception ping


【解决方案1】:

您应该登录InnerException 以了解实际情况:

catch (PingException ex)
{
    Console.WriteLine(ex.InnerException);
    ConnectionStatus = "Disconnected-Exception";
}

输出如下输出:

System.Net.Sockets.SocketException (11001): No such host is known.
   at System.Net.Dns.InternalGetHostByName(String hostName)
   at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
   at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)

ping 命令也失败了(确保结果符合预期):

PS C:\Users\user> ping https://www.google.com/
Ping request could not find host https://www.google.com/. Please check the name and try again.

这意味着主机 https://www.google.com/ 未启用 HTTPS 的 ICMP 回复。您可以查看Can you get a reply from a HTTPS site using the Ping command? 以获得关于为什么会发生这种情况的一个很好的解释。

您也可以在System.Net.NetworkInformation.Ping.Send() 文档中验证这一点:

PingException

发送或接收 ICMP 消息时引发异常。有关引发的确切异常,请参阅内部异常。

请尝试 ping 主机名 www.google.com

Ping myPing = new Ping();
String host = "www.google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();

string ConnectionStatus;

Ping ping = new Ping();
try
{
    PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
    if (reply.Status == IPStatus.Success)
        ConnectionStatus = "Connected";
    else
        ConnectionStatus = "Disconnected";
    }
catch (PingException ex)
{
    Console.WriteLine(ex.InnerException);
    ConnectionStatus = "Disconnected-Exception";
}

Console.WriteLine(ConnectionStatus);

不输出异常且连接成功:

Connected

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多