C#實現(xiàn)掃描局域網(wǎng)內的所有IP和端口
更新時間:2022年12月28日 08:44:28 作者:芝麻粒兒
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)掃描局域網(wǎng)內的所有IP和端口的功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實踐過程
效果

代碼
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 掃描局域網(wǎng)IP地址
/// <summary>
/// 掃描局域網(wǎng)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 = "局域網(wǎng)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對象,以便獲得局域網(wǎng)組名和計算機名
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)
{
//遍歷局域網(wǎng)中的工作組,并顯示在下拉列表控件中
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 = "局域網(wǎng)端口掃描";
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#實現(xiàn)掃描局域網(wǎng)內的所有IP和端口的文章就介紹到這了,更多相關C#掃描局域網(wǎng)IP端口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
快速解決C# android base-64 字符數(shù)組的無效長度問題
下面小編就為大家?guī)硪黄焖俳鉀QC# android base-64 字符數(shù)組的無效長度問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
C# 計算標準偏差相當于Excel中的STDEV函數(shù)實例
下面小編就為大家?guī)硪黄狢# 計算標準偏差相當于Excel中的STDEV函數(shù)實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01

