一.界面设计
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"

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

被折叠的 条评论
为什么被折叠?



