0

I am trying to get the IP's of devices attached to my network(WLAN to which i am connected) .Firstly i used Command Line in Win8 and pinging there serially while knowing my own IP (incrementing it each time and pinging ) .To get programmatically do it like WnetWatcher I am utilizing Ping Class by calling a function passing attempts=4 and timeout=3 but a blue screen saying PROCESS_HAS_BLOCKED_PAGESand found it an underlying API issue. Anyone has a better idea than this to get all the devices IP's because several threads at SO finds it using Dns Class but that works for a single PC(mine) .

1). What is the laternate of Ping and if it's Ping then how to get around API issue.

2).Also, how can i get the Router IP ,so that I maybe able to run the loop for other IP's on an network or is there any better alternative to it?

public static void Ping(string host, int attempts, int timeout) {

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


             }

    }
Community
  • 1
  • 1
Khan
  • 185
  • 1
  • 4
  • 15

1 Answers1

1

1) You may call SendAsync method of the Ping class instead of Send to avoid blocking:

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

And take care of the ping response in the PingCompleted EventHandler delegate:

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) To get your Router IP or gateway:

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;
}
mgigirey
  • 1,027
  • 9
  • 13