一、编写端口扫描器程序
打开Visual Studio,选择Windows 窗体应用
(一)单线程实现扫描
窗体设计
代码实现
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Scan1
{
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("");
Scan1();
}
private void Scan1()
{
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;
Scan1();
progressBar1.Value = i;
label5.Text = i.ToString();
}
while (!OK)
{
OK = true;
Scan1();
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
namespace Scan2
{
public partial class Form1 : Form
{
private int startPort;
private int endPort;
private string IP;
private int port;
private bool[] done = new bool[65536];
public Form1()
{
InitializeComponent();
listBox1.Items.Add("欢迎使用端口扫描器v1.0!");
this.Text = "端口扫描器v1.0";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void start_Port(object sender, EventArgs e)
{
label4.Text = textBox2.Text;
try
{
//string转int,如果字符串为空,则抛出ArgumentNullException异常;
//如果字符串内容不是数字,则抛出FormatException异常;
startPort = int.Parse(textBox2.Text);
}
catch
{
}
}
//获取结束端口
private void end_Port(object sender, EventArgs e)
{
label6.Text = textBox3.Text;
try
{
endPort = int.Parse(textBox3.Text);
}
catch
{
}
}
//获取IP地址
private void get_IP(object sender, EventArgs e)
{
IP = textBox1.Text;
}
//IP检验
private bool judge_IP()
{
int n = IP.Length;
int[] x = new int[5] { 0, 0, 0, 0, 0 };
int sum = 0;
int j = 0;
for (int i = 0; i < n; i++)
{
if (IP[i] == '.')
{
x[j] = sum;
sum = 0;
j++;
continue;
}
if (j == 4 || (IP[i] == '.' && IP[i + 1] == '.'))
{
return false;
}
sum = sum * 10 + (IP[i] - '0');
}
if (j == 3)
{
x[j] = sum;
for (int i = 0; i < 4; i++)
{
if (x[i] < 0 || x[i] > 255)
{
return false;
}
}
return true;
}
return false;
}
private void Scan()
{
int portnow = port;
//记录该端口已访问
done[portnow] = true;
try
{
TcpClient tcp = new TcpClient(IP, portnow);
//listBox1.Items.Add("端口 " + portnow.ToString() + " 开放!");
this.Invoke((EventHandler)delegate
{
listBox1.Items.Add("端口 " + portnow.ToString() + " 开放!");
});
}
catch
{
}
}
private void port_Scan()
{
//检查端口合法性
if ((startPort >= 0 && startPort <= 65535) && (endPort >= 0 && endPort <= 65535) && (startPort <= endPort))
{
if (IP != "" && judge_IP())
{
//listBox1.Items.Clear();
this.Invoke((EventHandler)delegate
{
listBox1.Items.Clear();
button1.Enabled = false;
listBox1.Items.Add("开始扫描……(该过程可能会等待一定时间!)");
});
//listBox1.Items.Add("开始扫描……(该过程可能会等待一定时间!)");
for (int i = startPort; i <= endPort; i++)
{
port = i;
//创建该端口的扫描线程
ThreadStart childScan = new ThreadStart(Scan);
Thread scanThread = new Thread(childScan);
scanThread.Start();
//使线程暂停100ms
System.Threading.Thread.Sleep(100);
//progressBar1.Value = i;
this.Invoke((EventHandler)delegate
{
progressBar1.Value = i;
label5.Text = i.ToString();
});
}
bool flag = true;
while (flag)
{
int i;
for (i = startPort; i <= endPort; i++)
{
if (!done[i])
{
break;
}
}
if (i == endPort + 1)
{
flag = false;
}
//线程暂停2000ms
System.Threading.Thread.Sleep(2000);
}
this.Invoke((EventHandler)delegate
{
listBox1.Items.Add("扫描结束!!!");
button1.Enabled = true;
});
//listBox1.Items.Add("扫描结束!!!");
}
else
{
MessageBox.Show("IP地址不合法!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
else
{
//错误消息反馈
MessageBox.Show("端口不合法!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
//开始扫描
private void button_Click(object sender, EventArgs e)
{
//port_Scan();
//创建线程,并创建 ThreadStart 委托对象
ThreadStart childScan = new ThreadStart(port_Scan);
Thread childThread = new Thread(childScan);
childThread.Start();
//显示端口扫描范围
progressBar1.Minimum = startPort;
progressBar1.Maximum = endPort;
//main_Thread();
//显示初始化端口信息
listBox1.Items.Clear();
listBox1.Items.Add("欢迎使用端口扫描器 v1.0!!!");
}
//用单一进程测试端口扫描
private void main_Thread()
{
listBox1.Items.Add("开始扫描……(该过程可能会等待一定时间!)");
for (int i = startPort; i <= endPort; i++)
{
progressBar1.Value = i;
label5.Text = i.ToString();
try
{
TcpClient tcp = new TcpClient(IP, i);
listBox1.Items.Add("端口 " + i.ToString() + " 开放!");
}
catch
{
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
}
}
结果显示: