发送多个 Ping 而不等待回复 Windows C#

发布于 2024-11-26 01:44:58 字数 288 浏览 1 评论 0原文

我目前正在为我最后一年的理学学士项目进行研究。最终产品将包括室内位置跟踪功能。传统的或最常用的方法似乎是 RSSI 三角测量,但我热衷于尝试提高 PING 方法的准确性,因为我认为这更适合可能遭受信号衰减的位置(我打算使用的位置)使用该设备可能会产生中等程度的无线电干扰)。

我想知道是否可以用 C# 编写软件来模仿 linux ping 实用程序的 ping 洪水功能(它可以发送多个 ping 而无需等待回复)。我假设使用多个 ping 并从第一个到最后一个计时将能够在较短的距离内使用该方法。

非常感

谢迪伦

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2024-12-03 01:44:58

试试这个希望它能正常工作

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

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
烂人 2024-12-03 01:44:58

您必须使用 Ping.SendAsync() 函数。正如 MSDN 页面中所述:

对此方法的每次调用都在一个单独的线程中执行
从线程池中自动分配。当异步
操作完成后,它会引发 PingCompleted 事件。要指定
当 SendAsync 引发事件时调用的方法,您必须添加一个
PingCompletedEventHandler 在调用之前委托给事件
发送异步。

下面是一个关于如何执行此操作的精彩示例: http://msdn.microsoft.com /en-us/library/a63bsyf0.aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文