C# 在局域网中获取活动 IP 地址

本文关键字:活动 IP 地址 获取 局域网 | 更新日期: 2023-09-27 18:32:51

C# 在

局域网中获取活动 IP 地址,这在控制台应用程序中工作正常,wen 它出现在基于表单的应用程序上 我收到错误,我是 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# 在局域网中获取活动 IP 地址

您将 IP"变量"用作类中的字段。

public String IP;

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

因此,您需要:

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

我建议你让所有成员都不是静态的,因为它们"属于"表单。

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

最后,这段代码起作用了。

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