C#端口掃描器的編寫方法
本文實例為大傢分享瞭C#端口掃描器的編寫代碼,供大傢參考,具體內容如下
一、項目設計
新建項目
整體設計
最終設計
二、單線程
1.代碼編寫
button
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace duankou { public partial class Form1 : Form { private string hostAddress; //起始端口 private int start; //終止端口 private int end; //端口號 private int port; //定義線程對象 private Thread scanThread; public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { //初始化 tbShow.Clear(); lb.Text = "0%"; //獲取ip地址和始末端口號 hostAddress = tbHost.Text; start = Int32.Parse(tbSPort.Text); end = Int32.Parse(tbEPort.Text); if (decideAddress()) // 端口合理 { //讓輸入的textbox隻讀,無法改變 tbHost.ReadOnly = true; tbSPort.ReadOnly = true; tbEPort.ReadOnly = true; //設置進度條的范圍 pb.Minimum = start; pb.Maximum = end; //顯示框顯示 tbShow.AppendText("端口掃描器 v1.0.0" + Environment.NewLine + Environment.NewLine); //調用端口掃描函數 PortScan(); } else { //若端口號不合理,彈窗報錯 MessageBox.Show("輸入錯誤,端口范圍為[0-65536]!"); } } catch { //若輸入的端口號為非整型,則彈窗報錯 MessageBox.Show("輸入錯誤,端口范圍為[0-65536]!"); } } /// <summary> /// 判斷端口是否合理 /// </summary> /// <returns></returns> private bool decideAddress() { //判斷端口號是否合理 if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)) return true; else return false; } private void PortScan() { double x; string xian; //顯示掃描狀態 tbShow.AppendText("開始掃描...(可能需要請您等待幾分鐘)" + Environment.NewLine + Environment.NewLine); //循環拋出線程掃描端口 for (int i = start; i <= end; i++) { x = (double)(i - start + 1) / (end - start + 1); xian = x.ToString("0%"); port = i; //調用端口i的掃描操作 Scan(); //進度條值改變 lb.Text = xian; lb.Refresh(); pb.Value = i; } tbShow.AppendText(Environment.NewLine + "掃描結束!" + Environment.NewLine); //輸入框textbox隻讀屬性取消 tbHost.ReadOnly = false; tbSPort.ReadOnly = false; tbEPort.ReadOnly = false; } /// <summary> /// 掃描某個端口 /// </summary> private void Scan() { int portnow = port; //創建TcpClient對象,TcpClient用於為TCP網絡服務提供客戶端連接 TcpClient objTCP = null; try { //用於TcpClient對象掃描端口 objTCP = new TcpClient(hostAddress, portnow); //掃描到則顯示到顯示框 tbShow.AppendText("端口 " + port + " 開放!" + Environment.NewLine); } catch { //未掃描到,則會拋出錯誤 } } } }
2.掃描結果
開始
1分鐘結束 沒有端口開放
三、多線程
1.代碼編寫
button
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace duankou { public partial class Form1 : Form { private string hostAddress; //起始端口 private int start; //終止端口 private int end; //端口號 private int port; //定義線程對象 private Thread scanThread; //定義端口狀態數據(開放則為true,否則為false) private bool[] done = new bool[65526]; private bool OK; public Form1() { InitializeComponent(); //不進行跨線程檢查 CheckForIllegalCrossThreadCalls = false; } private void label1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { //初始化 tbShow.Clear(); lb.Text = "0%"; //獲取ip地址和始末端口號 hostAddress = tbHost.Text; start = Int32.Parse(tbSPort.Text); end = Int32.Parse(tbEPort.Text); if (decideAddress()) // 端口合理 { //讓輸入的textbox隻讀,無法改變 tbHost.ReadOnly = true; tbSPort.ReadOnly = true; tbEPort.ReadOnly = true; //創建線程,並創建ThreadStart委托對象 Thread process = new Thread(new ThreadStart(PortScan)); process.Start(); //設置進度條的范圍 pb.Minimum = start; pb.Maximum = end; //顯示框顯示 tbShow.AppendText("端口掃描器 v1.0.0" + Environment.NewLine + Environment.NewLine); } else { //若端口號不合理,彈窗報錯 MessageBox.Show("輸入錯誤,端口范圍為[0-65536]!"); } } catch { //若輸入的端口號為非整型,則彈窗報錯 MessageBox.Show("輸入錯誤,端口范圍為[0-65536]!"); } } /// <summary> /// 判斷端口是否合理 /// </summary> /// <returns></returns> private bool decideAddress() { //判斷端口號是否合理 if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)) return true; else return false; } private void PortScan() { double x; string xian; //顯示掃描狀態 tbShow.AppendText("開始掃描...(可能需要請您等待幾分鐘)" + Environment.NewLine + Environment.NewLine); //循環拋出線程掃描端口 for (int i = start; i <= end; i++) { x = (double)(i - start + 1) / (end - start + 1); xian = x.ToString("0%"); port = i; //使用該端口的掃描線程 scanThread = new Thread(new ThreadStart(Scan)); scanThread.Start(); //使線程睡眠 System.Threading.Thread.Sleep(100); //進度條值改變 lb.Text = xian; pb.Value = i; } while (!OK) { OK = true; for (int i = start; i <= end; i++) { if (!done[i]) { OK = false; break; } } System.Threading.Thread.Sleep(1000); } tbShow.AppendText(Environment.NewLine + "掃描結束!" + Environment.NewLine); //輸入框textbox隻讀屬性取消 tbHost.ReadOnly = false; tbSPort.ReadOnly = false; tbEPort.ReadOnly = false; } /// <summary> /// 掃描某個端口 /// </summary> private void Scan() { int portnow = port; //創建線程變量 Thread Threadnow = scanThread; //掃描端口,成功則寫入信息 done[portnow] = true; //創建TcpClient對象,TcpClient用於為TCP網絡服務提供客戶端連接 TcpClient objTCP = null; try { //用於TcpClient對象掃描端口 objTCP = new TcpClient(hostAddress, portnow); //掃描到則顯示到顯示框 tbShow.AppendText("端口 " + port + " 開放!" + Environment.NewLine); } catch { //未掃描到,則會拋出錯誤 } } } }
2.掃描結果
開始
5秒結束
小結
多線程能夠有更好的CPU使用率,程序設計更簡單,更快的程序響應速度。
鏈接
C#單線程和多線程端口掃描器
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。