当前位置: 首页>>代码示例>>C#>>正文


C# Ping.SendTaskAsync方法代码示例

本文整理汇总了C#中System.Net.NetworkInformation.Ping.SendTaskAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Ping.SendTaskAsync方法的具体用法?C# Ping.SendTaskAsync怎么用?C# Ping.SendTaskAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.NetworkInformation.Ping的用法示例。


在下文中一共展示了Ping.SendTaskAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Ping

 public static async Task<PingReply> Ping(string hostNameOrIPAddress, int ttl = 0)
 {
     var pinger = new Ping();
     var options = new PingOptions(ttl, dontFragment: true);
     
     return await pinger.SendTaskAsync(hostNameOrIPAddress, 0x1388, DefaultSendBuffer, options);
 }
开发者ID:bhuvak,项目名称:NuGetOperations,代码行数:7,代码来源:NetworkHelpers.cs

示例2: ICMPTest

        private async Task<ICMPResult> ICMPTest(Server server)
        {
            Logging.Debug("Ping " + server.FriendlyName());
            if (server.server == "") return null;
            var result = new ICMPResult(server);
            try
            {
                var IP =
                    Dns.GetHostAddresses(server.server)
                        .First(
                            ip =>
                                ip.AddressFamily == AddressFamily.InterNetwork ||
                                ip.AddressFamily == AddressFamily.InterNetworkV6);
                var ping = new Ping();

                foreach (var _ in Enumerable.Range(0, Repeat))
                {
                    try
                    {
                        var reply = await ping.SendTaskAsync(IP, TimeoutMilliseconds);
                        if (reply.Status.Equals(IPStatus.Success))
                        {
                            result.RoundtripTime.Add((int?) reply.RoundtripTime);
                        }
                        else
                        {
                            result.RoundtripTime.Add(null);
                        }

                        //Do ICMPTest in a random frequency
                        Thread.Sleep(TimeoutMilliseconds + new Random().Next()%TimeoutMilliseconds);
                    }
                    catch (Exception e)
                    {
                        Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
                        Logging.LogUsefulException(e);
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
                Logging.LogUsefulException(e);
            }
            return result;
        }
开发者ID:CharlieQ1,项目名称:shadowsocks-windows,代码行数:46,代码来源:AvailabilityStatistics.cs

示例3: ICMPTest

 private async Task<List<DataList>> ICMPTest(Server server)
 {
     Logging.Debug("Ping " + server.FriendlyName());
     if (server.server == "") return null;
     var IP = Dns.GetHostAddresses(server.server).First(ip => (ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6));
     var ping = new Ping();
     var ret = new List<DataList>();
     foreach (var timestamp in Enumerable.Range(0, Repeat).Select(_ => DateTime.Now.ToString(DateTimePattern)))
     {
         //ICMP echo. we can also set options and special bytes
         try
         {
             var reply = await ping.SendTaskAsync(IP, Timeout);
             ret.Add(new List<KeyValuePair<string, string>>
             {
                 new KeyValuePair<string, string>("Timestamp", timestamp),
                 new KeyValuePair<string, string>("Server", server.FriendlyName()),
                 new KeyValuePair<string, string>("Status", reply?.Status.ToString()),
                 new KeyValuePair<string, string>("RoundtripTime", reply?.RoundtripTime.ToString())
                 //new KeyValuePair<string, string>("data", reply.Buffer.ToString()); // The data of reply
             });
             Thread.Sleep(Timeout + new Random().Next() % Timeout);
             //Do ICMPTest in a random frequency
         }
         catch (Exception e)
         {
             Logging.Error($"An exception occured while eveluating {server.FriendlyName()}");
             Logging.LogUsefulException(e);
         }
     }
     return ret;
 }
开发者ID:sammy2012,项目名称:shadowsocks-windows,代码行数:32,代码来源:AvailabilityStatistics.cs


注:本文中的System.Net.NetworkInformation.Ping.SendTaskAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。