C#实现扫描局域网内的所有IP和端口
短信预约 -IT技能 免费直播动态提醒
实践过程
效果
代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 实例化类对象及公共变量
private Thread myThread;
string StartIPAddress = "";
string EndIPAddress = "";
int intStrat =0; //开始扫描地址
int intEnd = 0; //终止扫描地址
string strIP = "";
string strflag = ""; //扫描到的IP地址
#endregion
private void button1_Click(object sender, EventArgs e)
{
try
{
if (button1.Text == "开始")
{
listView1.Items.Clear(); //清空ListView控件中的项
textBox1.Enabled = textBox2.Enabled = false;
strIP = "";
strflag = textBox1.Text;
StartIPAddress = textBox1.Text;
EndIPAddress = textBox2.Text;
//开始扫描地址
intStrat = Int32.Parse(StartIPAddress.Substring(StartIPAddress.LastIndexOf(".") + 1));
//终止扫描地址
intEnd = Int32.Parse(EndIPAddress.Substring(EndIPAddress.LastIndexOf(".") + 1));
//指定进度条最大值
progressBar1.Minimum = intStrat;
//指定进度条最小值
progressBar1.Maximum = intEnd;
//指定进度条初始值
progressBar1.Value = progressBar1.Minimum;
timer1.Start(); //开始运行计时器
button1.Text = "停止"; //设置按钮文本为停止
//使用自定义方法StartScan实例化线程对象
myThread = new Thread(new ThreadStart(this.StartScan));
myThread.Start(); //开始运行扫描IP的线程
}
else
{
textBox1.Enabled = textBox2.Enabled = true;
button1.Text = "开始"; //设置按钮文本为开始
timer1.Stop(); //停止运行计时器
//设置进度条的值为最大值
progressBar1.Value = intEnd;
if (myThread != null) //判断线程对象是否为空
{
//判断扫描IP地址的线程是否正在运行
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort(); //终止线程
}
}
}
}
catch { }
}
private void timer1_Tick(object sender, EventArgs e)
{
if (strIP != "") //判读是否有可用IP地址
{
if (strIP.IndexOf(',') == -1)
{
if (listView1.Items.Count > 0)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
//判断扫描到的IP地址是否与列表中的重复
if (listView1.Items[i].Text != strIP)
{
//向列表中添加扫描到的已用IP地址
listView1.Items.Add(strIP);
}
}
}
else
//向列表汇总添加扫描到的已用IP地址
listView1.Items.Add(strIP);
}
else
{
string[] strIPS = strIP.Split(',');
for (int i = 0; i < strIPS.Length; i++)
{
listView1.Items.Add(strIPS[i].ToString());
}
}
strIP = "";
}
for (int i = 0; i < listView1.Items.Count; i++)
listView1.Items[i].ImageIndex = 0;
if (progressBar1.Value < progressBar1.Maximum) //判断进度条的当前值是否超出其最大值
progressBar1.Value = Int32.Parse(strflag.Substring(strflag.LastIndexOf(".") + 1)); //将进度条的值加1
if (strflag == textBox2.Text) //判断正在扫描的IP地址是否是结束IP地址
{
timer1.Stop(); //停止运行计时器
textBox1.Enabled = textBox2.Enabled = true;
button1.Text = "开始"; //设置按钮文本为扫描
MessageBox.Show("IP地址扫描结束!");
}
}
private void Form1_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(0, 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 Form1
{
/// <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(Form1));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.listView1 = new System.Windows.Forms.ListView();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(4, -1);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(247, 73);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(9, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 0;
this.label1.Text = "开始地址:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 18);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(112, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "192.168.1.1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(71, 45);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(112, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "192.168.1.255";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(9, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 2;
this.label2.Text = "结束地址:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(189, 20);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(50, 44);
this.button1.TabIndex = 4;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(4, 276);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(247, 15);
this.progressBar1.TabIndex = 2;
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "ip.ico");
//
// listView1
//
this.listView1.FullRowSelect = true;
this.listView1.LargeImageList = this.imageList1;
this.listView1.Location = new System.Drawing.Point(4, 78);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(247, 192);
this.listView1.SmallImageList = this.imageList1;
this.listView1.StateImageList = this.imageList1;
this.listView1.TabIndex = 3;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(256, 293);
this.Controls.Add(this.listView1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.groupBox1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "局域网IP地址扫描";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ListView listView1;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 实例化类对象和公共变量
//实例化DirectoryEntry对象,以便获得局域网组名和计算机名
DirectoryEntry DEMain = new DirectoryEntry("WinNT:");
TcpClient TClient = null; //实例化连接侦听对象
private Thread myThread; //实例化线程对象
string strName = ""; //记录选择的计算机名称
int intflag = 0; //扫描到的端口号
int intport = 0; //记录已用端口号
int intstart = 0; //扫描的开始端口号
int intend = 0; //扫描的结束端口号
#endregion
private void Form1_Load(object sender, EventArgs e)
{
//遍历局域网中的工作组,并显示在下拉列表控件中
foreach (DirectoryEntry DEGroup in DEMain.Children)
{
comboBox1.Items.Add(DEGroup.Name);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (DirectoryEntry DEGroup in DEMain.Children)
{
//判断工作组名称
if (DEGroup.Name.ToLower() == comboBox1.Text.ToLower())
{
//遍历指定工作组中的所有计算机名称,并显示在ListBox控件中
foreach (DirectoryEntry DEComputer in DEGroup.Children)
{
if (DEComputer.Name.ToLower() != "schema")
{
listBox1.Items.Add(DEComputer.Name);
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear(); //清空ListView控件中的项
try
{
if (button1.Text == "扫描")
{
intport = 0; //初始化已用端口号
//指定进度条最大值
progressBar1.Minimum = Convert.ToInt32(textBox1.Text);
//指定进度条最小值
progressBar1.Maximum = Convert.ToInt32(textBox2.Text);
//指定进度条初始值
progressBar1.Value = progressBar1.Minimum;
timer1.Start(); //开始运行计时器
button1.Text = "停止"; //设置按钮文本为停止
intstart = Convert.ToInt32(textBox1.Text); //为开始扫描的端口号赋值
intend = Convert.ToInt32(textBox2.Text); //为结束扫描的端口号赋值
//使用自定义方法StartScan实例化线程对象
myThread = new Thread(new ThreadStart(this.StartScan));
myThread.Start(); //开始运行扫描端口号的线程
}
else
{
button1.Text = "扫描"; //设置按钮文本为扫描
timer1.Stop(); //停止运行计时器
//设置进度条的值为最大值
progressBar1.Value = Convert.ToInt32(textBox2.Text);
if (myThread != null) //判断线程对象是否为空
{
//判断扫描端口号的线程是否正在运行
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort(); //终止线程
}
}
}
}
catch { }
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (myThread != null) //判断线程对象是否为空
{
//判断扫描端口号的线程是否正在运行
if (myThread.ThreadState == ThreadState.Running)
{
myThread.Abort(); //终止线程
}
}
}
private void listBox1_Click(object sender, EventArgs e)
{
strName = listBox1.SelectedItem.ToString(); //记录选择的计算机名称
}
private void timer1_Tick(object sender, EventArgs e)
{
if (intport != 0) //判读是否有可用端口号
{
if (listView1.Items.Count > 0)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
//判断扫描到的端口号是否与列表中的重复
if (listView1.Items[i].Text != intport.ToString())
{
//向列表中添加扫描到的已用端口号
listView1.Items.Add(intport.ToString());
}
}
}
else
//向列表汇总添加扫描到的已用端口号
listView1.Items.Add(intport.ToString());
intport = 0;
}
if (progressBar1.Value < progressBar1.Maximum) //判断进度条的当前值是否超出其最大值
progressBar1.Value += 1; //将进度条的值加1
if (intflag == Convert.ToInt32(textBox2.Text)) //判断正在扫描的端口号是否是结束端口号
{
timer1.Stop(); //停止运行计时器
button1.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 Form1
{
/// <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.groupBox1 = new System.Windows.Forms.GroupBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.listView1 = new System.Windows.Forms.ListView();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.listBox1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.comboBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(8, 7);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(127, 225);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "选择计算机";
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(22, 81);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(94, 136);
this.listBox1.TabIndex = 3;
this.listBox1.Click += new System.EventHandler(this.listBox1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(10, 65);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 2;
this.label2.Text = "计算机:";
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(22, 39);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(94, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(10, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 0;
this.label1.Text = "工作组:";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.listView1);
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Controls.Add(this.textBox2);
this.groupBox2.Controls.Add(this.textBox1);
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Location = new System.Drawing.Point(142, 7);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(196, 225);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "扫描计算机已用端口号";
//
// button1
//
this.button1.Location = new System.Drawing.Point(147, 38);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(41, 23);
this.button1.TabIndex = 5;
this.button1.Text = "扫描";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(70, 43);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(17, 12);
this.label4.TabIndex = 4;
this.label4.Text = "到";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 39);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(51, 21);
this.textBox2.TabIndex = 3;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 39);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(51, 21);
this.textBox1.TabIndex = 2;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(8, 21);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(125, 12);
this.label3.TabIndex = 1;
this.label3.Text = "请设定端口扫描范围:";
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// listView1
//
this.listView1.Location = new System.Drawing.Point(16, 67);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(172, 150);
this.listView1.TabIndex = 6;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.SmallIcon;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(0, 236);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(342, 15);
this.progressBar1.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(342, 251);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "局域网端口扫描";
this.Load += new System.EventHandler(this.Form1_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ProgressBar progressBar1;
}
到此这篇关于C#实现扫描局域网内的所有IP和端口的文章就介绍到这了,更多相关C#扫描局域网IP端口内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341