C#在LAN中获取活动IP地址

时间:2014-12-03 07:19:13

标签: c# windows ip

C#在局域网中获取活动IP地址,这在控制台应用程序中工作正常,它来自基于表单的应用程序我遇到错误,我是c#pls帮助的新手。获取非静态字段的错误对象引用

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

                                     }

}

}

3 个答案:

答案 0 :(得分:1)

您使用IP&#34;变量&#34;作为你班上的领域。

public String IP;

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

所以你需要:

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

我建议你让所有成员都是非静态的,因为他们属于&#34;形式。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

最后这段代码可以使用。

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

    }