【问题标题】:Ping Class issue to get all Local IP's获取所有本地 IP 的 Ping 类问题
【发布时间】:2014-12-17 07:15:42
【问题描述】:

我正在尝试获取连接到我的网络的设备的 IP(我连接到的 WLAN)。首先我在 Win8 中使用命令行并在知道我自己的 IP 的同时连续 ping 那里(每次递增并 ping)。要像WnetWatcher 那样以编程方式进行操作,我正在通过调用传递attempts=4timeout=3 的函数来使用Ping Class,但蓝屏显示PROCESS_HAS_BLOCKED_PAGESfound 这是一个潜在的API 问题。任何人都有比这更好的主意来获取所有设备的 IP,因为 SO 的几个线程使用 Dns 类找到它,但这适用于单个 PC(我的)。

1)。 Ping 的后缀是什么,如果是 Ping 那么如何解决 API 问题。

2).另外,我怎样才能获得路由器 IP,以便我可以为网络上的其他 IP 运行循环,或者有更好的替代方案吗?

public static void Ping(字符串主机,int 尝试,int 超时) {

            new Thread(delegate()
            {
                try
                {
                    System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                    ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
                    ping.SendAsync(host, timeout, host);

                }
                catch
                {
                    // Do nothing and let it try again until the attempts are exausted.
                    // Exceptions are thrown for normal ping failurs like address lookup
                    // failed.  For this reason we are supressing errors.
                }
            }).Start();

    }
    private static void PingCompleted(object sender, PingCompletedEventArgs e)
    {
        string ip = (string)e.UserState;
        if (e.Reply != null && e.Reply.Status == IPStatus.Success)
        {
            // Logic for Ping Reply Success
          //  Console.WriteLine(String.Format("Host: {0} ping successful", ip));
            lstlocal.listViewItem //Error ...the intellisense is not accepting it here




        }
        else
        {
            // Logic for Ping Reply other than Success
        }
    }

       //function caller code from a button

        lstLocal.Items.Clear();

        lstLocal.FullRowSelect = true;
        bool value;
        for (int i = 0; i <= 254; i++)
              {

                        string ping_var = "192.168.1" + "." + i;
                        value = Ping(ping_var, 4, 3);
                        // MessageBox.Show("Ping response for"+ping_var +"is" + value);

                        if(value==true)
                        {
                           ListViewItem items=new ListViewItem(ping_var.ToString());
                           lstLocal.Items.Add(items);
                        }


             }

    }

【问题讨论】:

    标签: c# sockets ip-address ping wifi


    【解决方案1】:

    1) 您可以调用 Ping 类的 SendAsync 方法而不是 Send 以避免阻塞:

    public void Ping(string host, int attempts, int timeout)
    {
        for (int i = 0; i < attempts; i++)
        {
            new Thread(delegate()
            {
                try
                {
                    System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                    ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
                    ping.SendAsync(host, timeout, host);
                }
                catch
                {
                    // Do nothing and let it try again until the attempts are exausted.
                    // Exceptions are thrown for normal ping failurs like address lookup
                    // failed.  For this reason we are supressing errors.
                }
            }).Start();
        }
    }
    

    并在 PingCompleted EventHandler 委托中处理 ping 响应:

    private void PingCompleted(object sender, PingCompletedEventArgs e)
    {
        string ip = (string)e.UserState;
        if (e.Reply != null && e.Reply.Status == IPStatus.Success)
        {
            // Logic for Ping Reply Success
            ListViewItem item = new ListViewItem(ip);
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => 
                {
                    lstLocal.Items.Add(item);
                }));
            }
            else
            {
                lstLocal.Items.Add(item);
            }
            // Logic for Ping Reply Success
            // Console.WriteLine(String.Format("Host: {0} ping successful", ip));
        }
        else
        {
            // Logic for Ping Reply other than Success
        }
    }
    

    2) 获取路由器 IP 或网关:

    static string NetworkGateway()
    {
        string ip = null;
    
        foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (f.OperationalStatus == OperationalStatus.Up)
            {
                foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
                {
                    ip = d.Address.ToString();
                }
            }
        }
    
        Console.WriteLine(string.Format("Network Gateway: {0}", ip));
        return ip;
    }
    

    【讨论】:

    • 此外,它打印成功的消息'尝试'号。次数
    • 因为你的内循环for(int i = 0; i
    • 困惑...我应该在哪里使用listview 来获得成功的 PINGed IP。
    • 注释后“// Ping 回复成功的逻辑”。
    • 还有一点,超时以毫秒表示(100 就够了)。
    猜你喜欢
    • 2021-06-29
    • 2013-06-03
    • 2014-03-01
    • 2016-12-10
    • 2010-10-09
    • 2014-10-21
    • 2015-07-01
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多