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


C# Ping.SendPingAsync方法代码示例

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


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

示例1: PingAsync

        /// <summary>
        /// Sends the ICMP echo request to the specified IP address.
        /// </summary>
        /// <param name="addressIp">A destination IP address.</param>
        /// <param name="timeout">Optional timeout for connection.</param>
        /// <returns>ICMP echo request status.</returns>
        public async Task<IPStatus> PingAsync(IPAddress addressIp, TimeSpan? timeout = null)
        {
            var ping = new Ping();
            if (timeout.HasValue)
            {
                return await ping.SendPingAsync(addressIp, timeout.Value.Milliseconds)
                    .ContinueWith(pingTask => pingTask.Result.Status);
            }

            return await ping.SendPingAsync(addressIp)
                .ContinueWith(pingTask => pingTask.Result.Status);
        }
开发者ID:thecorrado,项目名称:Warden,代码行数:18,代码来源:IPinger.cs

示例2: Button_Click

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
           
           // Timer a = new Timer();
//
            //Ping pingSender = new Ping();
            ////for(int i=0;i<30;i++)
            ////{
            //  //  MessageBox.Show(adr);
               String adr ="172.16.16.10";
                IPAddress address = IPAddress.Parse(adr);
            //  //  pingSender.SendAsync(address,250,new PingOptions(5,true));
            // pingSender.SendAsync(address, 250);
           
            //pingSender.PingCompleted += (a, b) =>
            //{
            //    if (b.Reply.Status == IPStatus.Success)
            //    {
            //        MessageBox.Show(b.ToString());
            //    }
            //    else
            //    {
            //        MessageBox.Show("failed");
            //    }

            //};
            //PingReply reply = await pingSender.SendPingAsync(address);

            //if (reply.Status == IPStatus.Success)
            //{
            //    ListBox1.Items.Add(adr);
            //}
            //else
            //{
            //    ListBox2.Items.Add("Failed "+adr); // MessageBox.Show("falied" + adr);
            //}
            //}  
            AddressFamily d=new AddressFamily();

            try
            {
                Ping s=new Ping();
                var a= s.SendPingAsync(address).GetAwaiter();
               a.OnCompleted(() => MessageBox.Show(a.GetResult().Status.ToString()));
               //UdpClient udp=new UdpClient(8080);
               // udp.Connect(address,8080);
               // var a=new byte[]{1,2};
               // await udp.SendAsync(a,44);
               // TcpClient tcp = new TcpClient();
                //await tcp.ConnectAsync(adr, 8080);
               // MessageBox.Show(adr);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
开发者ID:prabaprakash,项目名称:Visual-Studio-2013,代码行数:60,代码来源:MainWindow.xaml.cs

示例3: PingButton_Click

        async private void PingButton_Click(object sender, EventArgs e)
        {
            List<string> urls = new List<string>()
            {
                "www.habitat-spokane.org",
                "www.partnersintl.org",
                "www.iassist.org",
                "www.fh.org",
                "www.worldvision.org"
            };

            IPStatus status;

            Func<string, Task<IPStatus>> func =
                    async (localUrl) =>
                    {
                        Random random = new Random();
                        Ping ping = new Ping();
                        PingReply pingReply =
                            await ping.SendPingAsync(localUrl);
                        return pingReply.Status;
                    };

            StatusLabel.Text = "Pinging…";

            foreach(string url in urls)
            {
                status = await func(url);
                StatusLabel.Text +=
                    [email protected]"{ Environment.NewLine 
                    }{ url }: { status.ToString() } ({
                    Thread.CurrentThread.ManagedThreadId 
                    })";
            }
        }
开发者ID:troubleminds,项目名称:TIP,代码行数:35,代码来源:Listing18.23.AwaitsAndControlFlow.cs

示例4: PingButton_Click

        async private void PingButton_Click(object sender, EventArgs e)
        {
            List<string> urls = new List<string>()
            {
                "www.habitat-spokane.org",
                "www.partnersintl.org",
                "www.iassist.org",
                "www.fh.org",
                "www.worldvision.org"
            };

            IPStatus status;

            Func<string, Task<IPStatus>> func =
                    async (localUrl) =>
                    {
                        Ping ping = new Ping();
                        PingReply pingReply =
                            await ping.SendPingAsync(localUrl);
                        return pingReply.Status;
                    };

            StatusLabel.Text = "Pinging…";

            foreach(string url in urls)
            {
                status = await func(url);
                StatusLabel.Text =
                    string.Format("{0}: {1} ({2})",
                    url, status.ToString(),
                    Thread.CurrentThread.ManagedThreadId);
            }
        }
开发者ID:rubycoder,项目名称:EssentialCSharp,代码行数:33,代码来源:Listing18.23.AwaitsAndControlFlow.cs

示例5: PingButton_Click

 async private void PingButton_Click(object sender, EventArgs e)
 {
     StatusLabel.Text = "Pinging…";
     Ping ping = new Ping();
     PingReply pingReply =
         await ping.SendPingAsync("yahoo.com");
     StatusLabel.Text = pingReply.Status.ToString();
 }
开发者ID:troubleminds,项目名称:TIP,代码行数:8,代码来源:Listing18.22.AsynchronousHighLatencyWPF.cs

示例6: ping

		public async Task<long> ping(){
			_latency = long.MaxValue;
            try {
                var pingSender = new Ping();
                var reply = await pingSender.SendPingAsync(uri.Host, maxTimeout);
                _latency = reply.Status == IPStatus.Success ? reply.RoundtripTime : long.MaxValue;
            }
            catch(PingException){ }
			return _latency;
		}
开发者ID:vivekkhandelwal,项目名称:CSharp-SpeedTest-Client,代码行数:10,代码来源:Server.cs

示例7: TryPingAsync

 async Task<bool> TryPingAsync(string hostName, Ping p, ICollection<long> pings) {
     try {
         var reply = await p.SendPingAsync(hostName, DefaultTimeout).ConfigureAwait(false);
         if ((reply != null) && (reply.Status == IPStatus.Success))
             pings.Add(reply.RoundtripTime);
         return true;
     } catch (Exception e) {
         this.Logger().FormattedWarnException(e);
         return false;
     }
 }
开发者ID:SIXNetworks,项目名称:withSIX.Desktop,代码行数:11,代码来源:PingProvider.cs

示例8: Poll

 public override async Task<State> Poll()
 {
     var ping = new Ping();
     var data = "B".PadRight(32, 'B');
     var buffer = Encoding.ASCII.GetBytes(data);
     PingReply reply = await ping.SendPingAsync(ServerName, pingTimeout , buffer, new PingOptions(128,true));
     
     _state = new State();
     _state.Url = string.Concat(this.Name, "-", reply.Address.ToString());
     _state.Status = reply.Status == IPStatus.Success ? "OK" : GetPingMessage(reply);
     return _state;
 }
开发者ID:bizoton19,项目名称:site-status-notification-csharp,代码行数:12,代码来源:Server.cs

示例9: Run_Click

 private async void Run_Click(object sender, RoutedEventArgs e)
 {
     Ping Pings = new Ping();
     int timeout = 500;
     int interval = 1000;
     int errors = 0;
     int errorLimit = 2;
     running = true;
     RunButton.Visibility = Visibility.Collapsed;
     StopButton.Visibility = Visibility.Visible;
     StatusTextBlock.Visibility = Visibility.Visible;
     while (running)
     {
         try {
             var res = await Pings.SendPingAsync("8.8.8.8", timeout);
             if (res.Status == IPStatus.Success)
             {
                 Console("Success, time: " + res.RoundtripTime + " ms");
                 SetIcon("Green");
                 StatusTextBlock.Text = "Success";
                 StatusTextBlock.Foreground = System.Windows.Media.Brushes.Green;
                 await Task.Delay(interval - (int)res.RoundtripTime);
             }
             else
             {
                 errors++;
                 if (errors >= errorLimit)
                 {
                     Console("Error");
                     SetIcon("Red");
                     StatusTextBlock.Text = "Error";
                     StatusTextBlock.Foreground = System.Windows.Media.Brushes.Red;
                 }
             }
         }
         catch
         {
             errors++;
             if (errors >= errorLimit)
             {
                 Console("Error");
                 SetIcon("Red");
                 StatusTextBlock.Text = "Error";
                 StatusTextBlock.Foreground = System.Windows.Media.Brushes.Red;
             }
             await Task.Delay(interval);
         }
     }
     SetIcon("White");
     RunButton.Visibility = Visibility.Visible;
     StopButton.Visibility = Visibility.Collapsed;
     StatusTextBlock.Visibility = Visibility.Collapsed;
 }
开发者ID:osjimenez,项目名称:PingTool,代码行数:53,代码来源:MainWindow.xaml.cs

示例10: timer1_Tick

 async private void timer1_Tick(object sender, EventArgs e)
 {
     Ping ping = new Ping();
     try
     {
         await ping.SendPingAsync("github.com");
         button1.Enabled = true;
     }
     catch (PingException)
     {
         button1.Enabled = false;
     }
 }
开发者ID:natashalysakova,项目名称:DiagnostikaPlus,代码行数:13,代码来源:SendBugReportForm.cs

示例11: PingOne

        public async Task<PingResult> PingOne(String address)
        {
            String hostname = String.Concat(address.TakeWhile(c => c != ':'));
            Ping pinger = new Ping();

            try
            {
                PingReply reply = await pinger.SendPingAsync(hostname, 1000);
                return new PingResult(address, reply);
            }
            catch (PingException)
            {
                return null;
            }
        }
开发者ID:BBQFACE,项目名称:renegadex-launcher,代码行数:15,代码来源:ServerList.cs

示例12: CheckInternetConnectionAsync

 public static async Task<bool> CheckInternetConnectionAsync(string URL = "http://www.google.com")
 {
     Ping myPing = new Ping();
     try
     {
         var pingReply = await myPing.SendPingAsync("google.com", 3000, new byte[32], new PingOptions(64, true));
         if (pingReply.Status == IPStatus.Success)
         {
             return true;
         }
     }
     catch
     {
         return false;
     }
     return false;
 }
开发者ID:dmtrKovalenko,项目名称:Meeting-broadcasting,代码行数:17,代码来源:ConnectivityChecker.cs

示例13: execute

        public override async Task<WorkerResult> execute()
        {
            bool pingable = false;
            Ping pinger = new Ping();
            try
            {
                PingReply reply = await pinger.SendPingAsync(this.Address.Host);
                pingable = reply.Status == IPStatus.Success;
                return new WorkerResult() { Success = pingable, Result = reply.Buffer };
            }
            catch (PingException e)
            {
                Debug.WriteLine("Error on pinging: " + this.Address);
                return new WorkerResult() { Success = false, Result = Encoding.ASCII.GetBytes(e.Message) };
            }

        }
开发者ID:mawasource,项目名称:asp.net-pingbot,代码行数:17,代码来源:PingWorker.cs

示例14: GetMachineOnlineStatusAsync

        /// <summary>
        /// Checks machine status via ping.
        /// </summary>
        /// <returns>Empty string if machine is online, exception message if ping failed.</returns>
        public static async Task<string> GetMachineOnlineStatusAsync(this Uri url, int timeout = 3000) {
            if (url.IsFile) {
                return string.Empty;
            }

            try {
                var ping = new Ping();
                var reply = await ping.SendPingAsync(url.Host, timeout);
                if (reply.Status != IPStatus.Success) {
                    return reply.Status.ToString();
                }
            } catch (PingException pex) {
                var pingMessage = pex.InnerException?.Message ?? pex.Message;
                if (!string.IsNullOrEmpty(pingMessage)) {
                    return pingMessage;
                }
            } catch (SocketException sx) {
                return sx.Message;
            }
            return string.Empty;
        }
开发者ID:Microsoft,项目名称:RTVS,代码行数:25,代码来源:Network.cs

示例15: SendPing

        public async Task<ushort> SendPing()
        {
            var ping = new Ping();
            var reply = await ping.SendPingAsync(EndPoint.Address);

            if (reply.Status != IPStatus.Success)
                throw new TimeoutException();

            return (ushort)reply.RoundtripTime;
        }
开发者ID:robhol,项目名称:Steam.Query,代码行数:10,代码来源:GameServer.cs


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