Send Multiple Pings without waiting for reply Windows C#

自闭症网瘾萝莉.ら 提交于 2019-12-11 00:49:05

问题


Im currently doing research towards my final year BSc project. The final product will include indoor location tracking functionality. The traditional, or most utilised method seems to be RSSI triangulation, but I am keen to attempt to improve the accuracy of the PING method as I think this would be better suited to locations that may suffer from signal attenuation (the locations I am intending to use the device may have a moderate ammount of radio interference).

I was wondering if it was possible to write software in c# that would mimick the ping flood ability of linux ping utility(which enable the sending of multiple pings without waiting for a reply). I hypothesize that using multiple pings and timing them from the first to the last will enable the usage of the method over shorter distances.

Many Thanks

Dylan


回答1:


try this hope it works fine

private ArrayList IPList()
        {
            string myipsplit = string.Empty;
            string myip =
                Dns.GetHostAddresses(
                    Dns.GetHostName())[0].ToString();
            string[] myiparray = myip.Split(new[] {'.'});
            for (int j = 1; j < myiparray.Length; j++)
                myipsplit += myiparray[j - 1] + ".";
            var sb = new ArrayList();
            MyPing ping = delegate(int id)
                              {
                                  string ls = myipsplit + id;
                                  var pingSender = new System.Net.NetworkInformation.Ping();
                                  PingReply reply = pingSender.Send(ls, 0);
                                  if (reply != null)
                                      if (reply.Status == IPStatus.Success)
                                          sb.Add(ls);
                              };
            var asyncResults = new IAsyncResult[0x100];
            for (int i = 1; i < 0x100; i++)
                asyncResults[i] = ping.BeginInvoke(i, null, null);
            for (int i = 1; i < 0x100; i++)
                ping.EndInvoke(asyncResults[i]);
            return sb;
        }

        #region Nested type: MyPing

        private delegate void MyPing(int id);

        #endregion



回答2:


You have to use the Ping.SendAsync() function. As stated in the MSDN page:

Each call to this method executes in a separate thread that is automatically allocated from the thread pool. When the asynchronous operation completes, it raises the PingCompleted event. To specify the method that is called when SendAsync raises the event, you must add a PingCompletedEventHandler delegate to the event before calling SendAsync.

Here's a splendid example on how to do it: http://msdn.microsoft.com/en-us/library/a63bsyf0.aspx



来源:https://stackoverflow.com/questions/6829141/send-multiple-pings-without-waiting-for-reply-windows-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!