using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace 多线程扫描网段
{
    public partial class Form1 : Form
    {
        private DateTime startTime;
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            int min = (int)this.numericUpDown4.Value,max = (int)this.numericUpDown5.Value;
            if (min>= max) { MessageBox.Show("网段输入错误!"); return; }
            startTime = DateTime.Now;
            string mask = this.numericUpDown1.Value.ToString() + "." + this.numericUpDown2.Value.ToString() + "." + this.numericUpDown3.Value.ToString();
            this.progressBar1.Minimum = min;
            this.progressBar1.Maximum = max;
            Thread[] threads=new Thread[max-min +1];
            while (min <= max)
            {
                Scan NewThreadScan = new Scan();
                NewThreadScan.IP = mask + "." + min.ToString();
                NewThreadScan.GetHostInfo = new HostInfo(newHostInfo);
                threads[min - 1] = new Thread(new ThreadStart(NewThreadScan.start));
                threads[min - 1].Start();
                min++;
            }   
        }
        public  void newHostInfo(string HostIP, string NewHostName)
        {
            this.setListBox(HostIP, NewHostName);
        }
        private void setListBox(string HostIp, string NewHostName)
        {
            if (listBox1.InvokeRequired && label4.InvokeRequired && progressBar1.InvokeRequired)
            {
                HostInfo b = new HostInfo(setListBox);
                Invoke(b, new object[] { HostIp, NewHostName });
            }
            else
            {
                lock (listBox1)
                {                    listBox1.Items.Add(HostIp + "" + NewHostName);
                    if (progressBar1.Value != progressBar1.Maximum)
                    {
                        progressBar1.Value++;
                    }
                    else
                    {
                        MessageBox.Show("成功完成检测!");
                        DateTime endTime = DateTime.Now;
                        TimeSpan timeSp = endTime - startTime;
                        label4.Text = timeSp.Seconds.ToString() + "秒";
                        progressBar1.Value = progressBar1.Minimum;
                        
                    }
                }
            }
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;namespace 多线程扫描网段
{
       class Scan
    {
        private string _IP;
        public string _HostName;
        public HostInfo GetHostInfo;
        public string IP 
        {
            get { return _IP; }
            set { _IP = value; }     
        }        public Scan()
        {        }        public void start()
        {
            IPAddress tempIp = IPAddress.Parse(_IP);
            try
            {
                IPHostEntry Host = Dns.GetHostByAddress(tempIp);
                _HostName = Host.HostName;
            }
            catch
            {
                _HostName = "主机没有反映";
            }
 
            if (this.GetHostInfo!=null)
            {
                this.GetHostInfo(_IP, _HostName);
            }
        }
    }
}