Ping 类问题以获取所有本地 IP

本文关键字:IP 获取 问题 Ping | 更新日期: 2023-09-27 18:34:23

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

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

2).另外,我如何获取路由器IP,以便我可能能够在网络上为其他IP运行循环,或者是否有更好的替代方案?

公共静态空 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);
                        }

             }
    }

Ping 类问题以获取所有本地 IP

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

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();
    }
}

并处理 PingDone 事件处理程序委托中的 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;
}