端口扫描器程序

本文介绍了如何在VS2017中创建一个端口扫描器程序,首先从界面设计开始,然后通过单线程实现基本的端口扫描。接着,通过引入多线程提升扫描速度,展示多线程在提高效率方面的优势。最后,总结了多线程在实际编程中的重要性,并提到在C#中使用委托解决跨线程控件操作的问题。

一.界面设计

1.vs2017建立工程文件
在这里插入图片描述
2.点击进入 From.cs[设计],可将界面设计如下:
在这里插入图片描述

二.端口扫描器(单线程)

1.编写代码

摆放完毕后,在窗口设计界面内,双击 button 按钮,可以转到代码编辑区。 以下是我的代码,也有部分注释。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;

namespace PortScan
{
   
   
    public partial class Form1 : Form
    {
   
   
        public Form1()
        {
   
   
            InitializeComponent();
        }
        //主机地址
        private string hostAddress;
        //起始端口
        private int start;
        //终止端口
        private int end;
        //端口号
        private int port;
        //定义线程对象
        private Thread scanThread;

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
   
   

        }


        private void textBox4_TextChanged(object sender, EventArgs e)
        {
   
   

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {
   
   

        }
        private void label4_Click(object sender, EventArgs e)
        {
   
   

        }


        private void button1_Click(object sender, EventArgs e)
        {
   
   
            try
            {
   
   
                //初始化
                textBox4.Clear();
                label5.Text = "0%";
                //获取ip地址和始末端口号
                hostAddress = textBox1.Text;
                start = Int32.Parse(textBox2.Text);
                end = Int32.Parse(textBox3.Text);
                if (decideAddress())
                {
   
   
                    //让输入的textbox只读,无法改变
                    textBox1.ReadOnly = true;
                    textBox2.ReadOnly = true;
                    textBox3.ReadOnly = true;
                    //设置进度条的范围
                    progressBar1.Minimum = start;
                    progressBar1.Maximum = end;
                    //显示框显示
                    textBox4.AppendText("端口扫描器 v1.0.0" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值