using System; 
 using System.Drawing; 
 using System.Collections; 
 using System.ComponentModel; 
 using System.Windows.Forms; 
 using System.Data; 
 using .Sockets; 
 using ; 
 using System.Threading ; namespace WindowsApplication31 
 { 
 /// <summary> 
 /// Form1 的摘要说明。 
 /// </summary> 
 public delegate void UpdateList ( string sIP , string sHostName ) ; 
 public class Form1 : System.Windows.Forms.Form 
 { 
  /// <summary> 
  /// 必需的设计器变量。 
  /// </summary> 
  private System.Windows.Forms.Button button1; 
  private System.Windows.Forms.ProgressBar progressBar1; 
  private System.ComponentModel.Container components = null; 
  private System.Windows.Forms.ListBox listBox1; 
  private System.Windows.Forms.GroupBox groupBox1; 
  private System.Windows.Forms.NumericUpDown numericUpDown1; 
  private System.Windows.Forms.NumericUpDown numericUpDown2; 
  private System.Windows.Forms.NumericUpDown numericUpDown3; 
  private System.Windows.Forms.NumericUpDown numericUpDown4; 
  private System.Windows.Forms.NumericUpDown numericUpDown5; 
  private System.Windows.Forms.Label label1; 
  private System.Windows.Forms.Label label2; 
  private System.Windows.Forms.Label label3; 
  private System.Windows.Forms.Label label4; 
  private System.DateTime StartTime;  public Form1() 
  { 
   // 
   // Windows 窗体设计器支持所必需的 
   // 
   InitializeComponent();   // 
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
   // 
  }  /// <summary> 
  /// 清理所有正在使用的资源。 
  /// </summary> 
  protected override void Dispose( bool disposing ) 
  { 
   if( disposing ) 
   { 
   if (components != null) 
   { 
    components.Dispose(); 
   } 
   } 
   base.Dispose( disposing ); 
  }  #region Windows Form Designer generated code 
  /// <summary> 
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
  /// 此方法的内容。 
  /// </summary> 
  private void InitializeComponent ( ) 
  { 
   this.button1 = new System.Windows.Forms.Button ( ) ; 
   this.progressBar1 = new System.Windows.Forms.ProgressBar ( ) ; 
   this.listBox1 = new System.Windows.Forms.ListBox ( ) ; 
   this.groupBox1 = new System.Windows.Forms.GroupBox ( ) ; 
   this.label2 = new System.Windows.Forms.Label ( ) ; 
   this.label1 = new System.Windows.Forms.Label ( ) ; 
   this.numericUpDown5 = new System.Windows.Forms.NumericUpDown ( ) ; 
   this.numericUpDown4 = new System.Windows.Forms.NumericUpDown ( ) ; 
   this.numericUpDown3 = new System.Windows.Forms.NumericUpDown ( ) ; 
   this.numericUpDown2 = new System.Windows.Forms.NumericUpDown ( ) ; 
   this.numericUpDown1 = new System.Windows.Forms.NumericUpDown ( ) ; 
   this.label3 = new System.Windows.Forms.Label ( ) ; 
   this.label4 = new System.Windows.Forms.Label ( ) ; 
   this.groupBox1.SuspendLayout ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown5 ) ).BeginInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown4 ) ).BeginInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown3 ) ).BeginInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown2 ) ).BeginInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown1 ) ).BeginInit ( ) ; 
   this.SuspendLayout ( ) ; 
   this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; 
   this.button1.Location = new System.Drawing.Point ( 304 , 41 ) ; 
    = "button1" ; 
   this.button1.Size = new System.Drawing.Size ( 75 , 36 ) ; 
   this.button1.TabIndex = 1 ; 
   this.button1.Text = "扫描" ; 
   this.button1.Click += new System.EventHandler ( this.button1_Click ) ; 
   this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom ; 
   this.progressBar1.Location = new System.Drawing.Point ( 0 , 316 ) ; 
    = "progressBar1" ; 
   this.progressBar1.Size = new System.Drawing.Size ( 414 , 23 ) ; 
   this.progressBar1.TabIndex = 2 ; 
   this.listBox1.ItemHeight = 12 ; 
   this.listBox1.Location = new System.Drawing.Point ( 0 , 82 ) ; 
    = "listBox1" ; 
   this.listBox1.Size = new System.Drawing.Size ( 414 , 232 ) ; 
   this.listBox1.TabIndex = 3 ; 
   this.groupBox1.Controls.AddRange ( new System.Windows.Forms.Control[] { 
             this.label2 , 
             this.label1 , 
             this.numericUpDown5 , 
             this.numericUpDown4 , 
             this.numericUpDown3 , 
             this.numericUpDown2 , 
             this.numericUpDown1 } ) ; 
   this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ; 
   this.groupBox1.Location = new System.Drawing.Point ( 2 , 8 ) ; 
    = "groupBox1" ; 
   this.groupBox1.Size = new System.Drawing.Size ( 282 , 70 ) ; 
   this.groupBox1.TabIndex = 1 ; 
   this.groupBox1.TabStop = false ; 
   this.groupBox1.Text = "选择IP地址" ; 
   this.label2.Location = new System.Drawing.Point ( 164 , 46 ) ; 
    = "label2" ; 
   this.label2.Size = new System.Drawing.Size ( 46 , 18 ) ; 
   this.label2.TabIndex = 11 ; 
   this.label2.Text = "终点:" ; 
   this.label1.Location = new System.Drawing.Point ( 164 , 22 ) ; 
    = "label1" ; 
   this.label1.Size = new System.Drawing.Size ( 46 , 18 ) ; 
   this.label1.TabIndex = 10 ; 
   this.label1.Text = "起点:" ; 
   this.numericUpDown5.Location = new System.Drawing.Point ( 224 , 44 ) ; 
   this.numericUpDown5.Maximum = new System.Decimal ( new int[] { 
           255 , 0 , 0 , 0} ) ; 
    = "numericUpDown5" ; 
   this.numericUpDown5.Size = new System.Drawing.Size ( 40 , 21 ) ; 
   this.numericUpDown5.TabIndex = 9 ; 
   this.numericUpDown4.Location = new System.Drawing.Point ( 224 , 20 ) ; 
   this.numericUpDown4.Maximum = new System.Decimal ( new int[] { 
           255 , 0 , 0 , 0} ) ; 
    = "numericUpDown4" ; 
   this.numericUpDown4.Size = new System.Drawing.Size ( 40 , 21 ) ; 
   this.numericUpDown4.TabIndex = 8 ; 
   this.numericUpDown3.Location = new System.Drawing.Point ( 108 , 32 ) ; 
   this.numericUpDown3.Maximum = new System.Decimal ( new int[] { 
           255 , 0 , 0 , 0} ) ; 
    = "numericUpDown3" ; 
   this.numericUpDown3.Size = new System.Drawing.Size ( 40 , 21 ) ; 
   this.numericUpDown3.TabIndex = 7 ; 
   this.numericUpDown3.value = new System.Decimal ( new int[] { 
            198 , 0 , 0 , 0} ) ; 
   this.numericUpDown2.Location = new System.Drawing.Point ( 62 , 32 ) ; 
   this.numericUpDown2.Maximum = new System.Decimal ( new int[] { 
           255 , 0 , 0 , 0} ) ; 
    = "numericUpDown2" ; 
   this.numericUpDown2.Size = new System.Drawing.Size ( 40 , 21 ) ; 
   this.numericUpDown2.TabIndex = 6 ; 
   this.numericUpDown2.value = new System.Decimal ( new int[] { 
            138 , 0 , 0 , 0} ) ; 
   this.numericUpDown1.Location = new System.Drawing.Point ( 18 , 32 ) ; 
   this.numericUpDown1.Maximum = new System.Decimal ( new int[] { 
           255 , 0 , 0 , 0} ) ; 
    = "numericUpDown1" ; 
   this.numericUpDown1.Size = new System.Drawing.Size ( 38 , 21 ) ; 
   this.numericUpDown1.TabIndex = 5 ; 
   this.numericUpDown1.value = new System.Decimal ( new int[] { 
            10 , 0 , 0 , 0} ) ; 
   this.label3.Location = new System.Drawing.Point ( 306 , 18 ) ; 
    = "label3" ; 
   this.label3.Size = new System.Drawing.Size ( 42 , 18 ) ; 
   this.label3.TabIndex = 11 ; 
   this.label3.Text = "耗时:" ; 
   this.label4.Location = new System.Drawing.Point ( 350 , 18 ) ; 
    = "label4" ; 
   this.label4.Size = new System.Drawing.Size ( 76 , 18 ) ; 
   this.label4.TabIndex = 12 ; 
   this.label4.Text = "0" ; 
   this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; 
   this.ClientSize = new System.Drawing.Size ( 414 , 339 ) ; 
   this.Controls.AddRange ( new System.Windows.Forms.Control[] { 
           this.label4 , 
           this.label3 , 
           this.groupBox1 , 
           this.listBox1 , 
           this.progressBar1 , 
           this.button1} ) ; 
   this.MaximizeBox = false ; 
    = "Form1" ; 
   this.Text = "扫描网络计算机" ; 
   this.groupBox1.ResumeLayout ( false ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown5 ) ).EndInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown4 ) ).EndInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown3 ) ).EndInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown2 ) ).EndInit ( ) ; 
   ( ( System.ComponentModel.ISupportInitialize ) ( this.numericUpDown1 ) ).EndInit ( ) ; 
   this.ResumeLayout ( false ) ; 
  } 
  #endregion  /// <summary> 
  /// 应用程序的主入口点。 
  /// </summary> 
  [STAThread] 
  static void Main() 
  { 
   Application.Run(new Form1()); 
  }  public class ping 
  { 
   public UpdateList ul ; 
   public string ip ; //接收传送来的IP地址字符串 
   public string HostName ;//向主进展传递对应IP地址是否在线数据 
   //这个过程用来判断传送来的IP地址对应计算机是否在线 
   public void scan ( ) 
   { 
   IPAddress myIP = IPAddress.Parse ( ip ) ; 
   try 
   { 
    IPHostEntry myHost = Dns.GetHostByAddress ( myIP ); 
    HostName = myHost.HostName.ToString ( ) ; 
   } 
   catch 
   { 
    HostName = "" ; 
   } 
   if (HostName == "") 
    HostName = " 主机没有响应!"; 
   if ( ul != null) 
    ul ( ip , HostName ) ; 
   } 
  }  void UpdateMyList ( string sIP , string sHostName ) 
  { 
   lock ( listBox1 ) 
   { 
   listBox1.Items.Add ( sIP + " " + sHostName ) ; 
   if ( progressBar1.value != progressBar1.Maximum ) 
   { 
    progressBar1.value++ ; 
   } 
   if ( progressBar1.value == progressBar1.Maximum ) 
   { 
    MessageBox.Show ( "成功完成检测!" , "提示" ) ; 
    DateTime EndTime = DateTime.Now ; 
    TimeSpan ts = EndTime - StartTime ; 
    label4.Text = ts.Seconds.ToString ( ) + "秒" ;   
    progressBar1.value = progressBar1.Minimum ; 
   } 
   } 
  } 
   
  private void button1_Click(object sender, System.EventArgs e) 
  { 
   listBox1.Items.Clear ( ) ; //清除扫描结果显示区域 
   StartTime = DateTime.Now ; //获取当前时间 
   string mask = numericUpDown1.value.ToString ( ) + "." + numericUpDown2.value.ToString ( ) + 
   "." + numericUpDown3.value.ToString ( ) + "." ; 
   int Min = ( int ) numericUpDown4.value ; 
   int Max = ( int ) numericUpDown5.value ; 
   if ( Min > Max ) 
   { 
   MessageBox.Show ( "输入的IP地址区间不合法,请检查!" , "错误!" ) ; 
   return ; 
   } 
   //判断输入的IP地址区间是否合法 
   int _ThreadNum = Max - Min + 1 ; 
   Thread[] mythread = new Thread [ _ThreadNum ] ; 
   //创建一个多个Thread实例 
   progressBar1.Minimum = Min ; 
   progressBar1.Maximum = Max + 1 ; 
   progressBar1.value = Min ; 
   int i ; 
   for (i = Min ; i <= Max ; i++ ) 
   { 
   int k = Max - i ; 
   ping HostPing = new ping ( ) ; //创建一个ping实例 
   HostPing.ip = mask + i.ToString ( ) ; 
   HostPing.ul = new UpdateList ( UpdateMyList ) ; //向这个ping实例中传递IP地址字符串 
   mythread[k] = new Thread ( new ThreadStart ( HostPing.scan ) ) ; //初始化一个线程实例 
   mythread[k].Start ( ) ; //启动线程 
   } 
  } } 
 }