目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
#region 实例化类对象及公共变量
private Thread myThread;
string StartIPAddress = "";
string EndIPAddress = "";
int intStrat =;
int intEnd =;
string strIP = "";
string strflag = "";
#endregion
private void button_Click(object sender, EventArgs e)
{
try
{
if (button.Text == "开始")
{
listView.Items.Clear();
textBox.Enabled = textBox2.Enabled = false;
strIP = "";
strflag = textBox.Text;
StartIPAddress = textBox.Text;
EndIPAddress = textBox.Text;
intStrat = Int.Parse(StartIPAddress.Substring(StartIPAddress.LastIndexOf(".") + 1));
intEnd = Int.Parse(EndIPAddress.Substring(EndIPAddress.LastIndexOf(".") + 1));
progressBar.Minimum = intStrat;
progressBar.Maximum = intEnd;
progressBar.Value = progressBar1.Minimum;
timer.Start();
button.Text = "停止";
myThread = new Thread(new ThreadStart(this.StartScan));
myThread.Start();
}
else
{
textBox.Enabled = textBox2.Enabled = true;
button.Text = "开始";
timer.Stop();
progressBar.Value = intEnd;
if (myThread != null)
{
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort();
}
}
}
}
catch { }
}
private void timer_Tick(object sender, EventArgs e)
{
if (strIP != "")
{
if (strIP.IndexOf(',') == -)
{
if (listView.Items.Count > 0)
{
for (int i =; i < listView1.Items.Count; i++)
{
if (listView.Items[i].Text != strIP)
{
listView.Items.Add(strIP);
}
}
}
else
listView.Items.Add(strIP);
}
else
{
string[] strIPS = strIP.Split(',');
for (int i =; i < strIPS.Length; i++)
{
listView.Items.Add(strIPS[i].ToString());
}
}
strIP = "";
}
for (int i =; i < listView1.Items.Count; i++)
listView.Items[i].ImageIndex = 0;
if (progressBar.Value < progressBar1.Maximum)
progressBar.Value = Int32.Parse(strflag.Substring(strflag.LastIndexOf(".") + 1));
if (strflag == textBox.Text)
{
timer.Stop();
textBox.Enabled = textBox2.Enabled = true;
button.Text = "开始";
MessageBox.Show("IP地址扫描结束!");
}
}
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
if (myThread != null)
{
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort();
}
}
}
#region 扫描局域网IP地址
private void StartScan()
{
for (int i = intStrat; i <= intEnd; i++)
{
string strScanIP = StartIPAddress.Substring(, StartIPAddress.LastIndexOf(".") + 1) + i.ToString();
IPAddress myScanIP = IPAddress.Parse(strScanIP);
strflag = strScanIP;
try
{
IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);
string strHostName = myScanHost.HostName.ToString();
if (strIP == "")
strIP += strScanIP + "->" + strHostName;
else
strIP += "," + strScanIP + "->" + strHostName;
}
catch { }
}
}
#endregion
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form));
this.groupBox = new System.Windows.Forms.GroupBox();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.button = new System.Windows.Forms.Button();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.timer = new System.Windows.Forms.Timer(this.components);
this.imageList = new System.Windows.Forms.ImageList(this.components);
this.listView = new System.Windows.Forms.ListView();
this.groupBox.SuspendLayout();
this.SuspendLayout();
this.groupBox.Controls.Add(this.button1);
this.groupBox.Controls.Add(this.textBox2);
this.groupBox.Controls.Add(this.label2);
this.groupBox.Controls.Add(this.textBox1);
this.groupBox.Controls.Add(this.label1);
this.groupBox.Location = new System.Drawing.Point(4, -1);
this.groupBox.Name = "groupBox1";
this.groupBox.Size = new System.Drawing.Size(247, 73);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(9, 21);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 0;
this.label.Text = "开始地址:";
this.textBox.Location = new System.Drawing.Point(71, 18);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(112, 21);
this.textBox.TabIndex = 1;
this.textBox.Text = "192.168.1.1";
this.textBox.Location = new System.Drawing.Point(71, 45);
this.textBox.Name = "textBox2";
this.textBox.Size = new System.Drawing.Size(112, 21);
this.textBox.TabIndex = 3;
this.textBox.Text = "192.168.1.255";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(9, 48);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 2;
this.label.Text = "结束地址:";
this.button.Location = new System.Drawing.Point(189, 20);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(50, 44);
this.button.TabIndex = 4;
this.button.Text = "开始";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.progressBar.Location = new System.Drawing.Point(4, 276);
this.progressBar.Name = "progressBar1";
this.progressBar.Size = new System.Drawing.Size(247, 15);
this.progressBar.TabIndex = 2;
this.timer.Interval = 1000;
this.timer.Tick += new System.EventHandler(this.timer1_Tick);
this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList.TransparentColor = System.Drawing.Color.Transparent;
this.imageList.Images.SetKeyName(0, "ip.ico");
this.listView.FullRowSelect = true;
this.listView.LargeImageList = this.imageList1;
this.listView.Location = new System.Drawing.Point(4, 78);
this.listView.Name = "listView1";
this.listView.Size = new System.Drawing.Size(247, 192);
this.listView.SmallImageList = this.imageList1;
this.listView.StateImageList = this.imageList1;
this.listView.TabIndex = 3;
this.listView.UseCompatibleStateImageBehavior = false;
this.listView.View = System.Windows.Forms.View.List;
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 293);
this.Controls.Add(this.listView);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.groupBox);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "局域网IP地址扫描";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form_FormClosed);
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.ProgressBar progressBar;
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.ImageList imageList;
private System.Windows.Forms.ListView listView;
}
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
#region 实例化类对象和公共变量
DirectoryEntry DEMain = new DirectoryEntry("WinNT:");
TcpClient TClient = null;
private Thread myThread;
string strName = "";
int intflag =;
int intport =;
int intstart =;
int intend =;
#endregion
private void Form_Load(object sender, EventArgs e)
{
foreach (DirectoryEntry DEGroup in DEMain.Children)
{
comboBox.Items.Add(DEGroup.Name);
}
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
listBox.Items.Clear();
foreach (DirectoryEntry DEGroup in DEMain.Children)
{
if (DEGroup.Name.ToLower() == comboBox.Text.ToLower())
{
foreach (DirectoryEntry DEComputer in DEGroup.Children)
{
if (DEComputer.Name.ToLower() != "schema")
{
listBox.Items.Add(DEComputer.Name);
}
}
}
}
}
private void button_Click(object sender, EventArgs e)
{
listView.Items.Clear();
try
{
if (button.Text == "扫描")
{
intport =;
progressBar.Minimum = Convert.ToInt32(textBox1.Text);
progressBar.Maximum = Convert.ToInt32(textBox2.Text);
progressBar.Value = progressBar1.Minimum;
timer.Start();
button.Text = "停止";
intstart = Convert.ToInt(textBox1.Text);
intend = Convert.ToInt(textBox2.Text);
myThread = new Thread(new ThreadStart(this.StartScan));
myThread.Start();
}
else
{
button.Text = "扫描";
timer.Stop();
progressBar.Value = Convert.ToInt32(textBox2.Text);
if (myThread != null)
{
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort();
}
}
}
}
catch { }
}
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
if (myThread != null)
{
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort();
}
}
}
private void listBox_Click(object sender, EventArgs e)
{
strName = listBox.SelectedItem.ToString();
}
private void timer_Tick(object sender, EventArgs e)
{
if (intport !=)
{
if (listView.Items.Count > 0)
{
for (int i =; i < listView1.Items.Count; i++)
{
if (listView.Items[i].Text != intport.ToString())
{
listView.Items.Add(intport.ToString());
}
}
}
else
listView.Items.Add(intport.ToString());
intport =;
}
if (progressBar.Value < progressBar1.Maximum)
progressBar.Value += 1;
if (intflag == Convert.ToInt(textBox2.Text))
{
timer.Stop();
button.Text = "扫描";
MessageBox.Show("端口扫描结束!");
}
}
#region 扫描端口号
private void StartScan()
{
while (true)
{
for (int i = intstart; i <= intend; i++)
{
intflag = i;
try
{
TClient = new TcpClient(strName, i);
intport = i;
}
catch { }
}
}
}
#endregion
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox = new System.Windows.Forms.GroupBox();
this.listBox = new System.Windows.Forms.ListBox();
this.label = new System.Windows.Forms.Label();
this.comboBox = new System.Windows.Forms.ComboBox();
this.label = new System.Windows.Forms.Label();
this.groupBox = new System.Windows.Forms.GroupBox();
this.button = new System.Windows.Forms.Button();
this.label = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.timer = new System.Windows.Forms.Timer(this.components);
this.listView = new System.Windows.Forms.ListView();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.groupBox.SuspendLayout();
this.groupBox.SuspendLayout();
this.SuspendLayout();
this.groupBox.Controls.Add(this.listBox1);
this.groupBox.Controls.Add(this.label2);
this.groupBox.Controls.Add(this.comboBox1);
this.groupBox.Controls.Add(this.label1);
this.groupBox.Location = new System.Drawing.Point(8, 7);
this.groupBox.Name = "groupBox1";
this.groupBox.Size = new System.Drawing.Size(127, 225);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "选择计算机";
this.listBox.FormattingEnabled = true;
this.listBox.ItemHeight = 12;
this.listBox.Location = new System.Drawing.Point(22, 81);
this.listBox.Name = "listBox1";
this.listBox.Size = new System.Drawing.Size(94, 136);
this.listBox.TabIndex = 3;
this.listBox.Click += new System.EventHandler(this.listBox1_Click);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(10, 65);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 2;
this.label.Text = "计算机:";
this.comboBox.FormattingEnabled = true;
this.comboBox.Location = new System.Drawing.Point(22, 39);
this.comboBox.Name = "comboBox1";
this.comboBox.Size = new System.Drawing.Size(94, 20);
this.comboBox.TabIndex = 1;
this.comboBox.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(10, 22);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(53, 12);
this.label.TabIndex = 0;
this.label.Text = "工作组:";
this.groupBox.Controls.Add(this.listView1);
this.groupBox.Controls.Add(this.button1);
this.groupBox.Controls.Add(this.label4);
this.groupBox.Controls.Add(this.textBox2);
this.groupBox.Controls.Add(this.textBox1);
this.groupBox.Controls.Add(this.label3);
this.groupBox.Location = new System.Drawing.Point(142, 7);
this.groupBox.Name = "groupBox2";
this.groupBox.Size = new System.Drawing.Size(196, 225);
this.groupBox.TabIndex = 1;
this.groupBox.TabStop = false;
this.groupBox.Text = "扫描计算机已用端口号";
this.button.Location = new System.Drawing.Point(147, 38);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(41, 23);
this.button.TabIndex = 5;
this.button.Text = "扫描";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(70, 43);
this.label.Name = "label4";
this.label.Size = new System.Drawing.Size(17, 12);
this.label.TabIndex = 4;
this.label.Text = "到";
this.textBox.Location = new System.Drawing.Point(88, 39);
this.textBox.Name = "textBox2";
this.textBox.Size = new System.Drawing.Size(51, 21);
this.textBox.TabIndex = 3;
this.textBox.Location = new System.Drawing.Point(16, 39);
this.textBox.Name = "textBox1";
this.textBox.Size = new System.Drawing.Size(51, 21);
this.textBox.TabIndex = 2;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(8, 21);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(125, 12);
this.label.TabIndex = 1;
this.label.Text = "请设定端口扫描范围:";
this.timer.Interval = 1000;
this.timer.Tick += new System.EventHandler(this.timer1_Tick);
this.listView.Location = new System.Drawing.Point(16, 67);
this.listView.Name = "listView1";
this.listView.Size = new System.Drawing.Size(172, 150);
this.listView.TabIndex = 6;
this.listView.UseCompatibleStateImageBehavior = false;
this.listView.View = System.Windows.Forms.View.SmallIcon;
this.progressBar.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar.Location = new System.Drawing.Point(0, 236);
this.progressBar.Name = "progressBar1";
this.progressBar.Size = new System.Drawing.Size(342, 15);
this.progressBar.TabIndex = 2;
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 251);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.groupBox);
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "局域网端口扫描";
this.Load += new System.EventHandler(this.Form_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form_FormClosed);
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.ListBox listBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.ComboBox comboBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.ProgressBar progressBar;
}