获取LAN上的所有IP地址

时间:2014-01-25 12:10:10

标签: c#

我有一个程序ping并获取局域网中活动的所有IP地址我使用csharp控制台模式对其进行编码问题是,当我将其置于模式形式时,在Spinwatch中出现错误并出现错误消息框

以下是

下面的代码和错误
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;

namespace pingagediffusiontest
 {
    public partial class Form1 : Form
     {
       public Form1()
         {
           InitializeComponent();
         }



       private void button1_Click(object sender, EventArgs e)
        { }
      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;

    static void Mains()
  {
     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) {
            Console.WriteLine(string.Concat("Active 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();

    }
    }

}  

错误1找不到类型或命名空间名称“SpinWait” 错误3找不到类型或命名空间名称“秒表” 错误4当前上下文中不存在名称“秒表”

2 个答案:

答案 0 :(得分:0)

只需添加

即可
using System.Diagnostics;

位于文件顶部。

答案 1 :(得分:0)

  

错误3找不到类型或命名空间名称“秒表”

您需要导入以下命名空间才能在程序中使用StopWatch

using System.Diagnostics;

根据@CodeCaster在评论中的建议 - 您缺少semicolon来终止Console.WriteLine()声明:

Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))  //missing semicolon

建议,因为您已声明已将控制台应用程序中的代码转换为Windows窗体应用程序,如果您可以使用Console.WriteLine()更改/替换所有MessageBox.Show()语句,那将会很不错如下所示:

MessageBox.Show(String.Concat("Non-active IP: ", e.Reply.Address.ToString()));