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


C# Ping.SendAsync方法代码示例

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


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

示例1: ListAllHosts

        public static void ListAllHosts(byte[] ipBase, Action<AddressStruct> itemCallback)
        {
            for (int b4 = 0; b4 <= 255; b4++)
            {
                var ip = ipBase[0] + "." + ipBase[1] + "." + ipBase[2] + "." + b4;

                var ping = new Ping();
                ping.PingCompleted += (o, e) =>
                {
                    if (e.Error == null && e.Reply.Status == IPStatus.Success)
                        if (itemCallback != null)
                        {
                            GetMacAddress(
                                e.Reply.Address,
                                (mac) =>
                                {
                                    itemCallback(new AddressStruct()
                                    {
                                        IPAddress = e.Reply.Address,
                                        MacAddress = mac
                                    });
                                });
                        }
                };
                ping.SendAsync(ip, null);
            }
        }
开发者ID:noant,项目名称:Pyrite,代码行数:27,代码来源:LANHelper.cs

示例2: Trace

 public static void Trace(IPAddress destination, Func<IPAddress, TracertNode, bool> callback)
 {
     if (destination == null)
     {
         throw new ArgumentNullException("destination");
     }
     else
     {
         if (callback == null)
         {
             throw new ArgumentNullException("callback");
         }
         else
         {
             var syncroot = new object();
             var buffer = new byte[INT_BufferSize];
             var options = new PingOptions(1, true);
             var ping = new Ping();
             ping.PingCompleted += (sender, e) =>
             {
                 var address = e.Reply.Address;
                 var status = e.Reply.Status;
                 back:
                 var done = !callback.Invoke(destination, new TracertNode(address, status, e.Reply.Options.Ttl)) || address.Equals(destination);
                 if (done)
                 {
                     try
                     {
                         if (ping != null)
                         {
                             ping.Dispose();
                         }
                     }
                     finally
                     {
                         ping = null;
                     }
                 }
                 else
                 {
                     lock (syncroot)
                     {
                         if (ping == null)
                         {
                             address = destination;
                             status = IPStatus.Unknown;
                             goto back;
                         }
                         else
                         {
                             options.Ttl++;
                             ping.SendAsync(destination, INT_TimeoutMilliseconds, buffer, options, null);
                         }
                     }
                 }
             };
             ping.SendAsync(destination, INT_TimeoutMilliseconds, buffer, options, null);
         }
     }
 }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:60,代码来源:TraceRoute.cs

示例3: PingAsync

        public Task<PingReply> PingAsync(string address)
        {
            var tcs = new TaskCompletionSource<PingReply>();

            t = new Thread(new ThreadStart(() =>
            {
                Ping ping = new Ping();
                ping.PingCompleted += (obj, sender) =>
                {
                    tcs.SetResult(sender.Reply);
                };

                ping.SendAsync(address, new object());

                if (tcs.Task.Result.Status.Equals(IPStatus.Success) || tcs.Task.Result.Status.Equals(IPStatus.TimedOut))
                {
                    isPinged = true;
                }
            }
              ));

            t.Start();

            return tcs.Task;
        }
开发者ID:0andy,项目名称:IPScan--findDevice,代码行数:25,代码来源:ScanIPDevice.cs

示例4: searchMethod

        /// <summary>
        /// 线程创建时调用此方法来搜索局域网主机
        /// </summary>
        private void searchMethod()
        {
            lock (obj)
            {
                //因为是刷新列表,所以要先清空
                if (result != null)
                {
                    result.Clear();
                }

                try
                {
                    //扫描整个网段
                    for (int i = 1; i < 256; i++)
                    {
                        //ping主机从而获取其IP地址和主机名
                        Ping myPing = new Ping();
                        myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);

                        //如果路由器不是这么设置的这个地方就要改~~~偷个懒~^_^
                        string pingIP = "10.0.2." + i.ToString();

                        //异步ping,防止主界面过长时间不响应
                        myPing.SendAsync(pingIP, null);
                        Thread.Sleep(1);
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("错误!");
                }
            }
        }
开发者ID:tevenfeng,项目名称:tcpFileTrans,代码行数:36,代码来源:LanEnum.cs

示例5: Submit

        /// <summary>
        /// Notifies the hub about the updatel
        /// </summary>
        public void Submit()
        {
            var ping = new Ping();

            string hostName = Hub.AbsoluteUri;
            string payload = Topic.AbsoluteUri;
            ping.SendAsync(hostName, TimeOut, payload.ToCharArray());
        }
开发者ID:spaetzel,项目名称:PubSubHubbubNet,代码行数:11,代码来源:PublishNotification.cs

示例6: PingAsync

 static Task<PingReply> PingAsync(string address)
 {
     var tcs = new TaskCompletionSource<PingReply>();
       Ping ping = new Ping();
       ping.PingCompleted += (obj, sender) => {
     tcs.SetResult(sender.Reply);
       };
       ping.SendAsync(address, new object());
       return tcs.Task;
 }
开发者ID:plausiblecontrol,项目名称:SuperPing,代码行数:10,代码来源:Program.cs

示例7: Connect

	public void Connect()
	{
		// We first try to ping twitch.tv to make sure it's reachable
		string data = "abcdefghijklmopqrstuvwxyz012345";
		byte[] buffer = Encoding.ASCII.GetBytes(data);
		int timeout = 5000;
		Ping ping = new Ping();
		ping.PingCompleted += HandleConnectionPingCompleted;
		PingOptions pingOption = new PingOptions(64, true);
		ping.SendAsync(Hostname, timeout, buffer, pingOption);
	}
开发者ID:GoupilRobin,项目名称:GGJ_HackSpam,代码行数:11,代码来源:TwitchIRC.cs

示例8: Ping

        public void Ping(Action completedAction)
        {
            Ping ping = new Ping();
            PingOptions pingOptions = new PingOptions();

            string data = "rotate";
            byte[] buffer = Encoding.ASCII.GetBytes(data);

            ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);
            ping.SendAsync(this.Host, 500, buffer, pingOptions, completedAction);
        }
开发者ID:SnakeSolidNL,项目名称:play-fonline,代码行数:11,代码来源:FOGameInfo.cs

示例9: Execute

        public void Execute()
        {
            //
            // 同期での送信.
            //
            var hostName = "localhost";
            var timeOut = 3000;

            var p = new Ping();
            var r = p.Send(hostName, timeOut);

            if (r.Status == IPStatus.Success)
            {
                Output.WriteLine("Ping.Send() Success.");
            }
            else
            {
                Output.WriteLine("Ping.Send() Failed.");
            }

            //
            // 非同期での送信.
            //
            hostName = "www.google.com";
            object userToken = null;

            p.PingCompleted += (s, e) =>
            {
                if (e.Cancelled)
                {
                    Output.WriteLine("Cancelled..");
                    return;
                }

                if (e.Error != null)
                {
                    Output.WriteLine(e.Error.ToString());
                    return;
                }

                if (e.Reply.Status != IPStatus.Success)
                {
                    Output.WriteLine("Ping.SendAsync() Failed");
                    return;
                }

                Output.WriteLine("Ping.SendAsync() Success.");
            };

            p.SendAsync(hostName, timeOut, userToken);
            Thread.Sleep(3000);
        }
开发者ID:devlights,项目名称:Sazare,代码行数:52,代码来源:PingSamples01.cs

示例10: ping_Async

 public void ping_Async(String sHostNameOrAddressToPing)
 {
     try
     {
         var pPing = new System.Net.NetworkInformation.Ping();
         pPing.PingCompleted += pPing_PingCompleted;
         pPing.SendAsync(sHostNameOrAddressToPing, sHostNameOrAddressToPing);
     }
     catch (Exception ex)
     {
         DI.log.error("in Ping: {0}", ex.Message);
     }
 }
开发者ID:pusp,项目名称:o2platform,代码行数:13,代码来源:Ping.cs

示例11: MyPing

        public string MyPing()
        {
            string ip1 = getIP();
             for(int i = 0; i < 255; i++)
            {
                Ping myPing=new Ping();
                myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
                // gethost ip 
                string pingIP =ip1  + i.ToString();
                myPing.SendAsync(pingIP, 1000, null);

            }
            return pingstr; 
        }
开发者ID:TinyNiko,项目名称:k3yManager,代码行数:14,代码来源:ServerWork.cs

示例12: ScanIPs

        public static void ScanIPs()
        {


            //string hostName = Dns.GetHostName(); // Retrive the Name of HOST

            //Console.WriteLine(hostName);

            // Get the IP

            //string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();

            string myIP = "192.168.1.1";
            var bytes = IPAddress.Parse(myIP).GetAddressBytes();
            // set the value here
            bytes[3] = 0;



            IPAddress ipAddress = new IPAddress(bytes);
            var IPaddress1 = IPAddress.Parse(myIP).GetAddressBytes()[0].ToString() + "." + IPAddress.Parse(myIP).GetAddressBytes()[1].ToString() + "." + IPAddress.Parse(myIP).GetAddressBytes()[2].ToString() + ".";

            Console.WriteLine("My IP Address is :" + myIP);
            Console.WriteLine("Network :" + IPaddress1);
            //Console.ReadLine();




            countdown = new CountdownEvent(1);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //string ipBase = "10.0.0.";
            for (int i = 1; i < 255; i++)
            {
                string ip = IPaddress1 + i.ToString();

                Ping p = new Ping();
                p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
                countdown.AddCount();
                p.SendAsync(ip, 100, ip);
            }
            countdown.Signal();
            countdown.Wait();
            sw.Stop();
            TimeSpan span = new TimeSpan(sw.ElapsedTicks);
            //Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount);
            //Console.WriteLine("\nPress any key to continue...");
            //Console.ReadKey();
        }
开发者ID:Ranthalion,项目名称:Network-Assessment,代码行数:50,代码来源:IPScanner.cs

示例13: PingAsync

 static Task<PingReply> PingAsync(string address)
 {
     var tcs = new TaskCompletionSource<PingReply>();
     Ping ping = new Ping();
     ping.PingCompleted += (obj, sender) =>
     {
         tcs.SetResult(sender.Reply);
     };
     ping.SendAsync(address, 200, new object());
     if (tcs.Task.Status.ToString() != "Success")
     {
         fail_count++;
     }
     return tcs.Task;
 }
开发者ID:leftor,项目名称:Multiping,代码行数:15,代码来源:frm_main.cs

示例14: RefreshServerList

        public static void RefreshServerList()
        {
            IPAddress ipAddress = Array.FindAll(Dns.GetHostAddresses(Dns.GetHostName()), a => a.AddressFamily == AddressFamily.InterNetwork).First();
            string[] baseIP = ipAddress.ToString().Split('.');

            for (int j = Int32.Parse(baseIP[2]); j < Int32.Parse(baseIP[2]) + 5; j++) {
                for (int i = 0; i < 255; i++) {
                    try {
                        IPAddress ip = IPAddress.Parse(baseIP[0] + "." + baseIP[1] + "." + j + "." + i);

                        Ping ping = new Ping();
                        ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
                        ping.SendAsync(ip, 600, ip);
                    } catch (Exception) { }
                }
            }
        }
开发者ID:maryus04,项目名称:ChatApplication,代码行数:17,代码来源:ServerFinder.cs

示例15: btnDomain2IP_Click

        private void btnDomain2IP_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                try
                {
                    Ping myPing = new Ping();
                    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
                    string hostName = dataGridView1[0, i].Value.ToString();
                    myPing.SendAsync(hostName, 1, i);
                }
                catch
                {

                }

            }
        }
开发者ID:henrydem,项目名称:yongfa365doc,代码行数:18,代码来源:frmWebIpWhois.cs


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