您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)基于C#如何實(shí)現(xiàn)端口掃描器的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
新建項(xiàng)目:
選擇Windows窗體項(xiàng)目應(yīng)用(.Net Framework):
設(shè)置項(xiàng)目名和路徑:
新建項(xiàng)目如下:
設(shè)置界面:
將tbShow設(shè)置為只讀:
雙擊按鈕,編寫其點(diǎn)擊事件:
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 Scan { public partial class Form1 : Form { //主機(jī)地址 private string hostAddress; //起始端口 private int start; //終止端口 private int end; //端口號(hào) private int port; //定義線程對(duì)象 private Thread scanThread; public Form1() { InitializeComponent(); } private void button1_Click_1(object sender, EventArgs e) { try { //初始化 tbShow.Clear(); lb.Text = "0%"; //獲取ip地址和始末端口號(hào) 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; //設(shè)置進(jìn)度條的范圍 pb.Minimum = start; pb.Maximum = end; //顯示框顯示 tbShow.AppendText("端口掃描器 v1.0.0" + Environment.NewLine + Environment.NewLine); //調(diào)用端口掃描函數(shù) PortScan(); } else { //若端口號(hào)不合理,彈窗報(bào)錯(cuò) MessageBox.Show("輸入錯(cuò)誤,端口范圍為[0-65536]!"); } } catch { //若輸入的端口號(hào)為非整型,則彈窗報(bào)錯(cuò) MessageBox.Show("輸入錯(cuò)誤,端口范圍為[0-65536]!"); } } /// <summary> /// 判斷端口是否合理 /// </summary> /// <returns></returns> private bool decideAddress() { //判斷端口號(hào)是否合理 if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)) return true; else return false; } private void PortScan() { double x; string xian; //顯示掃描狀態(tài) tbShow.AppendText("開始掃描...(可能需要請(qǐng)您等待幾分鐘)" + Environment.NewLine + Environment.NewLine); //循環(huán)拋出線程掃描端口 for (int i = start; i <= end; i++) { x = (double)(i - start + 1) / (end - start + 1); xian = x.ToString("0%"); port = i; //調(diào)用端口i的掃描操作 Scan(); //進(jìn)度條值改變 lb.Text = xian; lb.Refresh(); pb.Value = i; } tbShow.AppendText(Environment.NewLine + "掃描結(jié)束!" + Environment.NewLine); //輸入框textbox只讀屬性取消 tbHost.ReadOnly = false; tbSPort.ReadOnly = false; tbEPort.ReadOnly = false; } /// <summary> /// 掃描某個(gè)端口 /// </summary> private void Scan() { int portnow = port; //創(chuàng)建TcpClient對(duì)象,TcpClient用于為TCP網(wǎng)絡(luò)服務(wù)提供客戶端連接 TcpClient objTCP = null; try { //用于TcpClient對(duì)象掃描端口 objTCP = new TcpClient(hostAddress, portnow); //掃描到則顯示到顯示框 tbShow.AppendText("端口 " + port + " 開放!" + Environment.NewLine); } catch { //未掃描到,則會(huì)拋出錯(cuò)誤 } } } }
可發(fā)現(xiàn)速度十分緩慢,且不能拖拽。
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 Scan { public partial class Form1 : Form { //主機(jī)地址 private string hostAddress; //起始端口 private int start; //終止端口 private int end; //端口號(hào) private int port; //定義線程對(duì)象 private Thread scanThread; //定義端口狀態(tài)數(shù)據(jù)(開放則為true,否則為false) private bool[] done = new bool[65526]; private bool OK; public Form1() { InitializeComponent(); //不進(jìn)行跨線程檢查 CheckForIllegalCrossThreadCalls = false; } private void button1_Click_1(object sender, EventArgs e) { try { //初始化 tbShow.Clear(); lb.Text = "0%"; //獲取ip地址和始末端口號(hào) 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; //創(chuàng)建線程,并創(chuàng)建ThreadStart委托對(duì)象 Thread process = new Thread(new ThreadStart(PortScan)); process.Start(); //設(shè)置進(jìn)度條的范圍 pb.Minimum = start; pb.Maximum = end; //顯示框顯示 tbShow.AppendText("端口掃描器 v1.0.0" + Environment.NewLine + Environment.NewLine); } else { //若端口號(hào)不合理,彈窗報(bào)錯(cuò) MessageBox.Show("輸入錯(cuò)誤,端口范圍為[0-65536]!"); } } catch { //若輸入的端口號(hào)為非整型,則彈窗報(bào)錯(cuò) MessageBox.Show("輸入錯(cuò)誤,端口范圍為[0-65536]!"); } } /// <summary> /// 判斷端口是否合理 /// </summary> /// <returns></returns> private bool decideAddress() { //判斷端口號(hào)是否合理 if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)) return true; else return false; } private void PortScan() { double x; string xian; //顯示掃描狀態(tài) tbShow.AppendText("開始掃描...(可能需要請(qǐng)您等待幾分鐘)" + Environment.NewLine + Environment.NewLine); //循環(huán)拋出線程掃描端口 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); //進(jìn)度條值改變 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 + "掃描結(jié)束!" + Environment.NewLine); //輸入框textbox只讀屬性取消 tbHost.ReadOnly = false; tbSPort.ReadOnly = false; tbEPort.ReadOnly = false; } /// <summary> /// 掃描某個(gè)端口 /// </summary> private void Scan() { int portnow = port; //創(chuàng)建線程變量 Thread Threadnow = scanThread; //掃描端口,成功則寫入信息 done[portnow] = true; //創(chuàng)建TcpClient對(duì)象,TcpClient用于為TCP網(wǎng)絡(luò)服務(wù)提供客戶端連接 TcpClient objTCP = null; try { //用于TcpClient對(duì)象掃描端口 objTCP = new TcpClient(hostAddress, portnow); //掃描到則顯示到顯示框 tbShow.AppendText("端口 " + port + " 開放!" + Environment.NewLine); } catch { //未掃描到,則會(huì)拋出錯(cuò)誤 } } } }
可發(fā)現(xiàn)速度明顯變快,且能拖拽窗口。
感謝各位的閱讀!關(guān)于“基于C#如何實(shí)現(xiàn)端口掃描器”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。