快速局域网扫描,使用Ping类

有些小问题,主要是超时处理上.具体我都加了注释
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.Threading;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

namespace PingLan
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }
        DateTime StartTime;
        
public delegate void DelPing(object ip);

        
private void PingIP(object obj)
        {
            IPAddress ipadd 
= (IPAddress)obj;
            Ping p 
= new Ping();
            PingOptions po 
= new PingOptions();
            po.DontFragment 
= true;
            
//创建1个32字节的流
            string data = "012345678901234567890123456789ab";
            
byte[] buffer = Encoding.ASCII.GetBytes(data);
            
//超时,毫秒
            int timeout = 120;
            PingReply pr 
= p.Send(ipadd, timeout, buffer, po);
            
//ping成功
            if (pr.Status == IPStatus.Success)
            {
                
//线程调用
                if (listBox1.InvokeRequired)
                {
                    DelPing dp 
= new DelPing(PingIP);
                    
this.Invoke(dp, new object[] { ipadd });
                }
                
else
                {
                    listBox1.Items.Add(ipadd.ToString() 
+ " 在线");
                    
if (toolStripProgressBar1.Value != toolStripProgressBar1.Maximum)
                        toolStripProgressBar1.Value
++;
                    
//如果不检测超时IP,这里toolStripProgressBar1.Maximum要改
                    if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)
                    {
                        MessageBox.Show(
"检测完成!""完成", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        button1.Enabled 
= true;
                        DateTime EndTime 
= DateTime.Now;
                        
//时间间隔
                        TimeSpan ts = EndTime - StartTime;
                        toolStripStatusLabel1.Text 
= "用时: " + ts.Seconds.ToString();
                        toolStripProgressBar1.Value 
= toolStripProgressBar1.Minimum;
                    }
                }
            }
            
//如果检测超时,会很慢,而且貌似有点问题,列表框会无响应一段时间
            
//else//失败,IPStatus.TimedOut
            
//{
            
//    //线程调用
            
//    if (listBox1.InvokeRequired)
            
//    {
            
//        DelPing dp = new DelPing(PingIP);
            
//        this.Invoke(dp, new object[] { ipadd });
            
//    }
            
//    else
            
//    {
            
//        listBox1.Items.Add(ipadd.ToString() + " 离线");
            
//        if (toolStripProgressBar1.Value != toolStripProgressBar1.Maximum)
            
//            toolStripProgressBar1.Value++;
            
//        if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)
            
//        {
            
//            MessageBox.Show("检测完成!", "完成", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            
//            button1.Enabled = true;
            
//            DateTime EndTime = DateTime.Now;
            
//            //时间间隔
            
//            TimeSpan ts = EndTime - StartTime;
            
//            toolStripStatusLabel1.Text = "用时: " + ts.Seconds.ToString();
            
//            toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;
            
//        }

            
//    }
            
//}
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled 
= false;
            StartTime 
= DateTime.Now;
            
string host;
            IPAddress ipadds;
            listBox1.Items.Clear();
            
string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + ".";
            
int max = (int)numericUpDown5.Value;
            
int min = (int)numericUpDown4.Value;
            
if (min > max)
            {
                MessageBox.Show(
"IP地址范围错误!""错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                button1.Enabled 
= true;
                
return;
            }
            
else
            {
                
int num = max - min + 1;
                
//Thread[] tr = new Thread[num];
                toolStripProgressBar1.Minimum = min;
                
//如果不检测超时IP,这里toolStripProgressBar1.Maximum要改
                toolStripProgressBar1.Maximum = max + 1;
                toolStripProgressBar1.Value 
= min;
                
for (int i = min; i <= max; i++)
                {
                    
int k = max - i;
                    host 
= mask + i.ToString();
                    ipadds 
= IPAddress.Parse(host);
                    
//特别注明,如果线程中需要传参,一般,传1个参数,就用这个类,这个类参数必须是object的
                    ParameterizedThreadStart mypar = new ParameterizedThreadStart(PingIP);
                    
//经测试发现及时使用线程数组,和使用单个线程差不多速度
                    
//tr[k] = new Thread(mypar);
                    
//tr[k].Start(ipadds);
                     Thread th = new Thread(mypar);
                    th.Start(ipadds);
                }
            }
        }
    }
}
posted on 2008-05-23 09:09  雲淡風清  阅读(1134)  评论(0编辑  收藏  举报