【问题标题】:C# fetching Active Ip address in LANC#在局域网中获取活动IP地址
【发布时间】:2015-01-31 16:02:31
【问题描述】:

C# 获取 LAN 中的活动 IP 地址,这在控制台应用程序中运行良好,当它出现在基于表单的应用程序时我遇到错误,我是 c# 新手,请帮助。获取非静态字段的错误对象引用

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
    private static List<Ping> pingers = new List<Ping>();
    private static int instances = 0;

    private static object @lock = new object();

    private static int result = 0;
    private static int timeOut = 250;

    private static int ttl = 5;
    public  String IP;


    public Form1()
    {
        InitializeComponent();
    }





    private void button1_Click(object sender, EventArgs e)
            {

                IP = textBox1.Text;



                    string baseIP = "192.168.1.";

                    Console.WriteLine("Pinging 255 destinations of D-class in {0}*", baseIP);

                    CreatePingers(255);

                    PingOptions po = new PingOptions(ttl, true);
                    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                    byte[] data = enc.GetBytes("abababababababababababababababab");

                    SpinWait wait = new SpinWait();
                    int cnt = 1;

                    Stopwatch watch = Stopwatch.StartNew();

                                foreach (Ping p in pingers)
                                {
                                    lock (@lock)
                                    {
                                        instances += 1;
                                    }

                                    p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
                                    cnt += 1;
                                }

                    while (instances > 0)
                    {
                        wait.SpinOnce();
                    }

                    watch.Stop();

                    DestroyPingers();

                    Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result);
                    Console.ReadKey();  
            }



                        public static void Ping_completed(object s, PingCompletedEventArgs e)

                    {
                        lock (@lock)
                        {
                            instances -= 1;
                        }

                        if (e.Reply.Status == IPStatus.Success)
                        {
                            IP  = e.Reply.Address.ToString();
                            result += 1;
                        }
                        else
                        {
                            //Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
                        }
                    }


                            private static void CreatePingers(int cnt)
                            {
                                for (int i = 1; i <= cnt; i++)
                                {
                                    Ping p = new Ping();
                                    p.PingCompleted += Ping_completed;
                                    pingers.Add(p);
                                }
                            }

                                private static void DestroyPingers()
                                    {
                                    foreach (Ping p in pingers)
                                        {
                                             p.PingCompleted -= Ping_completed;
                                             p.Dispose();
                                        }

                                        pingers.Clear();

                                     }

}

}

【问题讨论】:

    标签: c# windows ip


    【解决方案1】:

    您收到此错误是因为 IP 被声明为非静态字段并在静态方法中使用。

    【讨论】:

      【解决方案2】:

      您在课堂上使用 IP“变量”作为字段。

      public String IP;
      

      但是您不能访问静态方法中的字段。据我所知,您的 3 种方法不需要是静态的。我猜这是从控制台应用程序复制/粘贴的结果,您在其中将方法编码为静态:)

      所以你需要:

      • 通过删除 static 关键字使您的方法成为非静态方法,或者
      • 通过添加 static 关键字使所有字段(在静态方法中访问的)静态

      我建议您将所有成员设为非静态成员,因为它们“属于”表单。

      【讨论】:

      • 您是否尝试过从您发送的代码文件中的所有方法和变量中删除静态关键字? ;)
      【解决方案3】:

      这段代码终于可以工作了。

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.Net.NetworkInformation;
      using System.Threading;
      using System.Diagnostics;
      using System.Net;
      
      
      namespace WindowsFormsApplication3
      {
      public partial class Form1 : Form
      {
          private static List<Ping> pingers = new List<Ping>();
          private static int instances = 0;
      
          private static object @lock = new object();
      
          private static int result = 0;
          private static int timeOut = 250;
      
          private static int ttl = 5;
          private static string[] IPs=new string[100];
      
          public Form1()
          {
              InitializeComponent();
      
          }
      
          private void button1_Click(object sender, EventArgs e)
          {
              string baseIP = "192.168.1.";
      
              IP.Items.Add("Pinging 255 destinations of D-class in " + baseIP+"xxx");
      
              CreatePingers(255);
      
      
              PingOptions po = new PingOptions(ttl, true);
              System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
              byte[] data = enc.GetBytes("abababababababababababababababab");
      
              SpinWait wait = new SpinWait();
              int cnt = 1;
      
              Stopwatch watch = Stopwatch.StartNew();
      
              foreach (Ping p in pingers)
              {
                  lock (@lock)
                  {
                      instances += 1;
                  }
      
                  p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
                  cnt += 1;
      
              }
      
      
          }
      
          public static void Ping_completed(object s, PingCompletedEventArgs e)
          {
              lock (@lock)
              {
                  instances -= 1;
              }
      
              if (e.Reply.Status == IPStatus.Success)
              {
      
                  Console.WriteLine(string.Concat("Active IP: ", e.Reply.Address.ToString()));
      
                  IPs[result] = e.Reply.Address.ToString();
                  result += 1;
              }
              else
              {
                  //Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
              }
          }
      
      
          private static void CreatePingers(int cnt)
          {
              for (int i = 1; i <= cnt; i++)
              {
                  Ping p = new Ping();
                  p.PingCompleted += Ping_completed;
                  pingers.Add(p);
              }
          }
      
          private static void DestroyPingers()
          {
              foreach (Ping p in pingers)
              {
                  p.PingCompleted -= Ping_completed;
                  p.Dispose();
              }
      
              pingers.Clear();
      
          }
      

      【讨论】:

        猜你喜欢
        • 2012-01-10
        • 2016-02-26
        • 2011-12-02
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多