C#实现扫描局域网内的所有IP和端口

.NET
527
0
0
2023-07-12
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
#region 实例化类对象及公共变量
private Thread myThread;
string StartIPAddress = "";
string EndIPAddress = "";
int intStrat =; //开始扫描地址
int intEnd =; //终止扫描地址
string strIP = "";
string strflag = ""; //扫描到的IP地址
#endregion
private void button_Click(object sender, EventArgs e)
{
try
{
if (button.Text == "开始")
{
listView.Items.Clear(); //清空ListView控件中的项
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 = "停止"; //设置按钮文本为停止
//使用自定义方法StartScan实例化线程对象
myThread = new Thread(new ThreadStart(this.StartScan));
myThread.Start(); //开始运行扫描IP的线程
}
else
{
textBox.Enabled = textBox2.Enabled = true;
button.Text = "开始"; //设置按钮文本为开始
timer.Stop(); //停止运行计时器
//设置进度条的值为最大值
progressBar.Value = intEnd;
if (myThread != null) //判断线程对象是否为空
{
//判断扫描IP地址的线程是否正在运行
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort(); //终止线程
}
}
}
}
catch { }
}
private void timer_Tick(object sender, EventArgs e)
{
if (strIP != "") //判读是否有可用IP地址
{
if (strIP.IndexOf(',') == -)
{
if (listView.Items.Count > 0)
{
for (int i =; i < listView1.Items.Count; i++)
{
//判断扫描到的IP地址是否与列表中的重复
if (listView.Items[i].Text != strIP)
{
//向列表中添加扫描到的已用IP地址
listView.Items.Add(strIP);
}
}
}
else
//向列表汇总添加扫描到的已用IP地址
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)); //将进度条的值加1
if (strflag == textBox.Text) //判断正在扫描的IP地址是否是结束IP地址
{
timer.Stop(); //停止运行计时器
textBox.Enabled = textBox2.Enabled = true;
button.Text = "开始"; //设置按钮文本为扫描
MessageBox.Show("IP地址扫描结束!");
}
}
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
if (myThread != null) //判断线程对象是否为空
{
//判断扫描IP地址的线程是否正在运行
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort(); //终止线程
}
}
}
#region 扫描局域网IP地址
/// <summary>
/// 扫描局域网IP地址
/// </summary>
private void StartScan()
{
//扫描的操作
for (int i = intStrat; i <= intEnd; i++)
{
string strScanIP = StartIPAddress.Substring(, StartIPAddress.LastIndexOf(".") + 1) + i.ToString();
//转换成IP地址
IPAddress myScanIP = IPAddress.Parse(strScanIP);
strflag = strScanIP;
try
{
//址获取DNS主机信息
IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);
//获取主机名
string strHostName = myScanHost.HostName.ToString();
if (strIP == "")
strIP += strScanIP + "->" + strHostName;
else
strIP += "," + strScanIP + "->" + strHostName;
}
catch { }
}
}
#endregion
}
partial class Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
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();
//
// groupBox
//
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;
//
// label
//
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 = "开始地址:";
//
// textBox
//
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";
//
// textBox
//
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";
//
// label
//
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 = "结束地址:";
//
// button
//
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);
//
// progressBar
//
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;
//
// timer
//
this.timer.Interval = 1000;
this.timer.Tick += new System.EventHandler(this.timer1_Tick);
//
// imageList
//
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");
//
// listView
//
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;
//
// Form
//
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对象,以便获得局域网组名和计算机名
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())
{
//遍历指定工作组中的所有计算机名称,并显示在ListBox控件中
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(); //清空ListView控件中的项
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); //为结束扫描的端口号赋值
//使用自定义方法StartScan实例化线程对象
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; //将进度条的值加1
if (intflag == Convert.ToInt(textBox2.Text)) //判断正在扫描的端口号是否是结束端口号
{
timer.Stop(); //停止运行计时器
button.Text = "扫描"; //设置按钮文本为扫描
MessageBox.Show("端口扫描结束!");
}
}
#region 扫描端口号
/// <summary>
/// 扫描端口号
/// </summary>
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
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
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();
//
// groupBox
//
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 = "选择计算机";
//
// listBox
//
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);
//
// label
//
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 = "计算机:";
//
// comboBox
//
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);
//
// label
//
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 = "工作组:";
//
// groupBox
//
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 = "扫描计算机已用端口号";
//
// button
//
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);
//
// label
//
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 = "到";
//
// textBox
//
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;
//
// textBox
//
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;
//
// label
//
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 = "请设定端口扫描范围:";
//
// timer
//
this.timer.Interval = 1000;
this.timer.Tick += new System.EventHandler(this.timer1_Tick);
//
// listView
//
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;
//
// progressBar
//
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;
//
// Form
//
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;
}