本文介绍了从SendPingAsync结果检索ping主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从字符串(地址)列表中ping主机。一切正常工作,但当主机无法访问ping返回 Result.Address :0.0.0.0而不是我传入SendPingAsync函数的现有地址或主机名。
这是我的代码:

  var pingTasks = addresses.Select(address => 
{
return new Ping()。SendPingAsync(address);
});

await Task.WhenAll(pingTasks);

StringBuilder pingResultBuilder = new StringBuilder();

foreach(var pingReply in pingTasks)
{
pingResultBuilder.Append(pingReply.Result.Address);
pingResultBuilder.Append();

pingResultBuilder.Append(pingReply.Result.Status);
pingResultBuilder.Append();

pingResultBuilder.Append(pingReply.Result.RoundtripTime.ToString());
pingResultBuilder.AppendLine();
}

Console.WriteLine(pingResultBuilder.ToString());

我发现解决方案,但是我需要在 SendPingAsync 函数,而不是 SendAsync
如何从 pingReply.Result 中检索已传递的主机名/ ip地址

解决方案

您可以使用 SendAsync 编写扩展方法。

  public static class MyExtensions 
{
public static Task< PingResult> SendTaskAsync(此Ping ping,字符串地址)
{
var tcs = new TaskCompletionSource< PingResult>();
PingCompletedEventHandler response = null;
response =(s,e)=>
{
ping.PingCompleted - = response;
tcs.SetResult(new PingResult(){Address = address,Reply = e.Reply});
};
ping.PingCompleted + = response;
ping.SendAsync(address,address);
return tcs.Task;
}

public class PingResult
{
public string Address {set;得到; }
public PingReply回复{set;得到; }
}
}

现在可以使用它:

  var pingTasks = addresses.Select(address => 
{
返回新的Ping()。SendTaskAsync );
});

var replies = await Task.WhenAll(pingTasks);

StringBuilder pingResultBuilder = new StringBuilder();

foreach(var pingReply in reply)
{
pingResultBuilder.Append(pingReply.Address);
pingResultBuilder.Append();

pingResultBuilder.Append(pingReply.Reply.Status);
pingResultBuilder.Append();

pingResultBuilder.Append(pingReply.Reply.RoundtripTime.ToString());
pingResultBuilder.AppendLine();
}

Console.WriteLine(pingResultBuilder.ToString());


I'm pinging hosts from list of strings (addresses). Everything works perfectly but when host is unreachable ping returns Result.Address: "0.0.0.0" instead of existing address or hostname which I passed into SendPingAsync function.Here is my code:

            var pingTasks = addresses.Select(address =>
            {
                return new Ping().SendPingAsync(address);
            });

            await Task.WhenAll(pingTasks);

            StringBuilder pingResultBuilder = new StringBuilder();

            foreach (var pingReply in pingTasks)
            {
                pingResultBuilder.Append(pingReply.Result.Address);
                pingResultBuilder.Append("    ");

                pingResultBuilder.Append(pingReply.Result.Status);
                pingResultBuilder.Append("    ");

                pingResultBuilder.Append(pingReply.Result.RoundtripTime.ToString());
                pingResultBuilder.AppendLine();
            }

            Console.WriteLine(pingResultBuilder.ToString());

I found solution here but I need to do the same thing in SendPingAsync function instead of SendAsync.How can I retrieve passed host name / ip address from pingReply.Result?

解决方案

You can write an extension method using SendAsync.

public static class MyExtensions
{
    public static Task<PingResult> SendTaskAsync(this Ping ping, string address)
    {
        var tcs = new TaskCompletionSource<PingResult>();
        PingCompletedEventHandler response = null;
        response = (s, e) =>
        {
            ping.PingCompleted -= response;
            tcs.SetResult(new PingResult() { Address = address, Reply = e.Reply });
        };
        ping.PingCompleted += response;
        ping.SendAsync(address, address);
        return tcs.Task;
    }

    public class PingResult
    {
        public string Address { set; get; }
        public PingReply Reply { set; get; }
    }
}

You can now use it like:

var pingTasks = addresses.Select(address =>
{
    return new Ping().SendTaskAsync(address);
});

var replies = await Task.WhenAll(pingTasks);

StringBuilder pingResultBuilder = new StringBuilder();

foreach (var pingReply in replies)
{
    pingResultBuilder.Append(pingReply.Address);
    pingResultBuilder.Append("    ");

    pingResultBuilder.Append(pingReply.Reply.Status);
    pingResultBuilder.Append("    ");

    pingResultBuilder.Append(pingReply.Reply.RoundtripTime.ToString());
    pingResultBuilder.AppendLine();
}

Console.WriteLine(pingResultBuilder.ToString());

这篇关于从SendPingAsync结果检索ping主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 09:51