Ping.SendAsync()始终成功,即使客户端无法在cmd中进行ping操作 [英] Ping.SendAsync() always successful, even if client is not pingable in cmd

查看:294
本文介绍了Ping.SendAsync()始终成功,即使客户端无法在cmd中进行ping操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Ping.SendAsync()方法对C#中的某些IP地址执行ping操作.我有一个具有5个IP地址的treeView,并为每个节点使用SendAsync()方法.在这里您可以看到:

I try to ping some IP addresses in C# using Ping.SendAsync() method. I have a treeView with 5 IP addresses and use the SendAsync() method for each node. Here you can see:

private void Form1_Load(object sender, EventArgs e)
{
    byte[] buffer = Encoding.ASCII.GetBytes(".");
    PingOptions options = new PingOptions(50, true);
    AutoResetEvent reset = new AutoResetEvent(false);
    Ping ping = new Ping();
    ping.PingCompleted += new PingCompletedEventHandler(ping_Complete);

    foreach (TreeNode node in treeView1.Nodes)
    {
        ping.SendAsync(node.Text, 5000, buffer, options, reset);
    }
}

private void ping_Complete(object sender, PingCompletedEventArgs k)
{
    foreach (TreeNode node in treeView1.Nodes)
    {
        PingReply reply = k.Reply;

        if (reply.Status == IPStatus.Success)
        {
            node.Text = node.Text + " (OK)";
        }

        else
        {
            node.Text = node.Text + " (FAILED)";
        }
    }
}

问题是,ping总是成功.我有2个在线且可ping通的客户.其他3个处于脱机状态且无法ping通(在cmd中,我无法ping通这些客户端).因此它应该显示以下内容:

The problem is, the ping is always successful. I got 2 clients which were online and pingable. The other 3 are offline and not pingable (in cmd I couldn't ping these clients). So it should display this:

IP1 (OK)
IP2 (FAILED)
IP3 (FAILED)
IP4 (OK)
IP5 (FAILED)

但是输出是(OK)"的5倍.

But the output is 5 times "(OK)".

有什么建议吗? :)

推荐答案

我认为Jon对您的问题有正确的解释.

I think Jon has the correct explanation to your problem.

我的建议是您改用SendPingAsync方法.它返回一个Task<PingReply>您可以等待(您还需要使方法异步):

My suggestion is that you use the SendPingAsync method instead; it returns a Task<PingReply> which you can await (you also need to make your method asynchronous):

private async void Form1_Load(object sender, EventArgs e)
{
    byte[] buffer = Encoding.ASCII.GetBytes(".");
    PingOptions options = new PingOptions(50, true);
    AutoResetEvent reset = new AutoResetEvent(false);
    Ping ping = new Ping();
    ping.PingCompleted += new PingCompletedEventHandler(ping_Complete);

    foreach (TreeNode node in treeView1.Nodes)
    {
        var reply = await ping.SendPingAsync(node.Text, 5000, buffer, options, reset);
        if (reply.Status == IPStatus.Success)
        {
            node.Text = node.Text + " (OK)";
        }

        else
        {
            node.Text = node.Text + " (FAILED)";
        }
    }
}

(请注意,此方法需要.NET 4.5)

(note that this approach requires .NET 4.5)

正如Mike Z在评论中指出的那样,以上方法将串行而不是并行执行ping.如果要并行执行这些操作,则可以执行以下操作:

As pointed out by mike z in the comments, the approach above will perform the pings serially, not in parallel. If you want to do them in parallel, you can do something like that:

private async void Form1_Load(object sender, EventArgs e)
{
    byte[] buffer = Encoding.ASCII.GetBytes(".");
    PingOptions options = new PingOptions(50, true);
    AutoResetEvent reset = new AutoResetEvent(false);
    Ping ping = new Ping();
    ping.PingCompleted += new PingCompletedEventHandler(ping_Complete);

    var tasks = List<Task>();
    foreach (TreeNode node in treeView1.Nodes)
    {
        var task = PingAndUpdateNodeAsync(ping, node);
        tasks.Add(task);
    }

    await Task.WhenAll(tasks);
}

private async Task PingAndUpdateNodeAsync(Ping ping, TreeNode node)
{
    var reply = await ping.SendPingAsync(node.Text, 5000, buffer, options, reset);
    if (reply.Status == IPStatus.Success)
    {
        node.Text = node.Text + " (OK)";
    }
    else
    {
        node.Text = node.Text + " (FAILED)";
    }
}

这篇关于Ping.SendAsync()始终成功,即使客户端无法在cmd中进行ping操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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