目录
前期准备单线程端口扫描多线程端口扫描引用前期准备
首先进入vs新建一个项目
选择窗体应用程序
在From1.cs将应用界面设计好,需要注意对文本框编辑性和按钮的属性要一一确认,具体操作可参考鄙人上一篇博客
单线程端口扫描
对于单线程的端口扫描可参考如下代码
using System;using System.Windows.Forms;using ;using .Sockets;namespace PortScan{public partial class Form1 : Form{public Form1(){InitializeComponent();}//自定义变量private int port;//记录当前扫描的端口号private string Address;//记录扫描的系统地址private bool[] done = new bool[65536];//记录端口的开放状态private int start;//记录扫描的起始端口private int end;//记录扫描的结束端口private bool OK;private void button1_Click(object sender, EventArgs e){label4.Text = textBox2.Text;label6.Text = textBox3.Text;progressBar1.Minimum = Int32.Parse(textBox2.Text);progressBar1.Maximum = Int32.Parse(textBox3.Text);listBox1.Items.Clear();listBox1.Items.Add("端口扫描器v1.0.");listBox1.Items.Add("");PortScan();}private void PortScan(){start = Int32.Parse(textBox2.Text);end = Int32.Parse(textBox3.Text);//判断输入端口是否合法if((start>=0&&start<=65536)&&(end>=0&&end<=65536)&&(start<=end)){listBox1.Items.Add("开始扫描:这个过程可能需要等待几分钟!");Address = textBox1.Text;for(int i = start; i <= end; i++){port = i;Scan();progressBar1.Value = i;label5.Text = i.ToString();}while (!OK){OK = true;for(int i = start; i <= end; i++){if (!done[i]){OK = false;break;}}}listBox1.Items.Add("扫描结束!");}else{MessageBox.Show("输入错误,端口范围为[0,65536]");}}//连接端口private void Scan(){int portnow = port;done[portnow] = true;TcpClient objTCP = null;try{objTCP = new TcpClient(Address, portnow);listBox1.Items.Add("端口"+portnow.ToString()+"开放");}catch{}}}}
运行以后出现上图所示窗口,依次输出指定信息后便可开始扫描
扫描成功!
多线程端口扫描
对于多线程端口扫描,可参考如下代码
using System;using System.Windows.Forms;using ;using .Sockets;using System.Threading;namespace PortScan{public partial class Form1 : Form{public Form1(){InitializeComponent();}//自定义变量private int port;//记录当前扫描的端口号private string Address;//记录扫描的系统地址private bool[] done = new bool[65536];//记录端口的开放状态private int start;//记录扫描的起始端口private int end;//记录扫描的结束端口private bool OK;private Thread scanThread; //将输入的起始端口放到进度条的开始位置private void label4_TextChanged(object sender, EventArgs e){label4.Text = textBox2.Text;}//将输入的结束地址放到进度条的结束位置private void label6_TextChanged(object sender, EventArgs e){label6.Text = textBox3.Text;}private void button1_Click(object sender, EventArgs e){label4_TextChanged(sender, e);label6_TextChanged(sender, e);//创建线程,并创建ThreadStart委托对象Thread procss = new Thread(new ThreadStart(PortScan));procss.Start();//显示端口扫描范围progressBar1.Minimum = Int32.Parse(textBox2.Text);progressBar1.Maximum = Int32.Parse(textBox3.Text);//显示框的初始化listBox1.Items.Clear();listBox1.Items.Add("端口扫描器v1.0.");listBox1.Items.Add("");}private void PortScan(){start = Int32.Parse(textBox2.Text);end = Int32.Parse(textBox3.Text);//检查端口的合法性if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)){listBox1.Items.Add("开始扫描:这个过程可能需要等待几分钟!");Address = textBox1.Text;for (int i = start; i <= end; i++){port = i;//对该端口进行扫描的线程scanThread = new Thread(Scan);scanThread.Start();//使线程睡眠System.Threading.Thread.Sleep(100);progressBar1.Value = i;label5.Text = i.ToString();}//未完成时情况while (!OK){OK = true;for (int i = start; i <= end; i++){if (!done[i]){OK = false;break;}}}listBox1.Items.Add("扫描结束!");System.Threading.Thread.Sleep(1000);}else{MessageBox.Show("输入错误,端口范围为[0,65536]");}}private void Scan(){int portnow = port;//创建线程变量Thread Threadnow = scanThread;done[portnow] = true;//创建TcpClient对象,TcpClient用于TCP网络服务提供客户端连接TcpClient objTCP = null;//扫描端口,成功就写入信息try{objTCP = new TcpClient(Address, portnow);listBox1.Items.Add("端口" + portnow.ToString() + "开放!");}catch{}}}}
多线程端口扫描的工作效率比单线程高很多,所以可以将端口数扩大范围,在这里扫描到80端口开放,说明此电脑正在进行http服务
接下来还有其她端口也处于开放状态,说明电脑正在运行其他服务选项
引用
本实验报告参考自
/qq_43279579/article/details/109686607
以上!
如果觉得《TCP编程-端口扫描器》对你有帮助,请点赞、收藏,并留下你的观点哦!