本文小编为大家详细介绍“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秒结束
读到这里,这篇“C#端口扫描器如何编写”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。