UDP端口联接:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ;
using .Sockets;namespace UDP端口号连接
{
/** <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/** <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /** <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} Windows Form Designer generated code#region Windows Form Designer generated code
/** <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(94, 135);
= "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
= "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /** <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
bool tcpListen = false ;
bool udpListen = false ;
.IPAddress myIpAddress = IPAddress.Parse("202.96.170.165");
.IPEndPoint myIpEndPoint = new IPEndPoint (myIpAddress,8000);
try
{
.Sockets.TcpClient tcpClient =new TcpClient ();
tcpClient.Connect(myIpEndPoint);
tcpListen=true;
}
catch{ }
try
{
.Sockets.UdpClient udpClient =new UdpClient ();
udpClient.Connect(myIpEndPoint);
udpListen=true;
}
catch{ }
if (tcpListen==false && udpListen==false)
{
MessageBox.Show("8000端口关闭!","提示");
}
else
MessageBox.Show("8000端口打开!","提示");
}
}
}DNS解析:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ;namespace DNS解析
{
/** <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/** <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /** <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} Windows Form Designer generated code#region Windows Form Designer generated code
/** <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 152);
= "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(211, 129);
this.richTextBox1.TabIndex = 8;
this.richTextBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 72);
= "button1";
this.button1.Size = new System.Drawing.Size(69, 42);
this.button1.TabIndex = 7;
this.button1.Text = "获取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 40);
= "textBox1";
this.textBox1.Size = new System.Drawing.Size(210, 20);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
= "label1";
this.label1.Size = new System.Drawing.Size(136, 23);
this.label1.TabIndex = 5;
this.label1.Text = "请输入主机名或域名:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 128);
= "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 9;
this.label2.Text = "IP地址列表:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 302);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox1,
this.button1,
this.textBox1,
this.label1,
this.label2});
= "Form1";
this.Text = "DNS解析";
this.ResumeLayout(false); }
#endregion /** <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Text = "";
try
{
IPHostEntry IPHost = Dns.GetHostByName (textBox1.Text);
IPAddress [ ] address = IPHost.AddressList;
//获取IP地址列表
foreach(IPAddress s in address)
{
//显示IP地址
richTextBox1.Text = s.ToString() + " " + richTextBox1.Text;
}
}
catch
{
MessageBox.Show("指定IP地址的网络主机不在线" ,"警告!");
}
}
}
}获得本地计算机名:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 获取本地计算机名称
{
/** <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/** <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /** <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} Windows Form Designer generated code#region Windows Form Designer generated code
/** <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(59, 171);
= "textBox2";
this.textBox2.Size = new System.Drawing.Size(173, 20);
this.textBox2.TabIndex = 11;
this.textBox2.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(109, 207);
= "button2";
this.button2.Size = new System.Drawing.Size(69, 35);
this.button2.TabIndex = 10;
this.button2.Text = "获取";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(59, 145);
= "label2";
this.label2.Size = new System.Drawing.Size(173, 23);
this.label2.TabIndex = 9;
this.label2.Text = "通过DNS获取的计算机名称:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 24);
= "label1";
this.label1.Size = new System.Drawing.Size(186, 23);
this.label1.TabIndex = 8;
this.label1.Text = "通过NetBios获取的计算机名称:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(58, 53);
= "textBox1";
this.textBox1.Size = new System.Drawing.Size(173, 20);
this.textBox1.TabIndex = 7;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(109, 89);
= "button1";
this.button1.Size = new System.Drawing.Size(69, 35);
this.button1.TabIndex = 6;
this.button1.Text = "获取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.button2,
this.label2,
this.label1,
this.textBox1,
this.button1});
= "Form1";
this.Text = "获取计算机名称";
this.ResumeLayout(false); }
#endregion /** <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
string hostName = System.Environment.MachineName;
textBox1.Text = hostName;
} private void button2_Click(object sender, System.EventArgs e)
{
string hostName = .Dns.GetHostName( );
textBox2.Text = hostName;
}
}
}获得网络计算机名:
namespace 获取网络主机名
{
/** <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/** <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /** <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} Windows Form Designer generated code#region Windows Form Designer generated code
/** <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 174);
= "label2";
this.label2.Size = new System.Drawing.Size(123, 23);
this.label2.TabIndex = 9;
this.label2.Text = "网络计算机名称:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(72, 40);
= "label1";
this.label1.Size = new System.Drawing.Size(160, 23);
this.label1.TabIndex = 8;
this.label1.Text = "请输入网络计算机IP地址:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(81, 198);
= "textBox2";
this.textBox2.Size = new System.Drawing.Size(120, 20);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(108, 119);
= "button1";
this.button1.Size = new System.Drawing.Size(68, 36);
this.button1.TabIndex = 6;
this.button1.Text = "获取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 72);
= "textBox1";
this.textBox1.Size = new System.Drawing.Size(123, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.label1,
this.textBox2,
this.button1,
this.textBox1});
= "Form1";
this.Text = "获取网络计算机名称";
this.ResumeLayout(false); }
#endregion /** <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
IPAddress myIP;
//判断IP地址的合法性
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("输入的IP地址不合法!" ,"警告!");
return;
}
//通过给定的IP地址,进行DNS查询,得到远程主机名
try
{
IPHostEntry myHost = Dns.GetHostByAddress(myIP);
textBox2.Text = myHost.HostName;
}
catch
{
MessageBox.Show( "错误信息:IP地址为" + textBox1.Text + "的主机没有响应" ,"警告!");
}
}
}
}
多线程扫描计算机:
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 );
} Windows Form Designer generated code#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 ( ) ; //启动线程
}
} }
}
















