C# 端口扫描
1.端口扫描原理
主要是利用套接字,来和远程主机的逐个端口进行连接,如果连接上说明该端口是开放的,否则是关闭的。当然现在各种安全技术发展迅速,使得这种方法扫描的端口可能不太准确,但是总得从最基本的学起吧~~
实现起来也不难,关键是看执行效率如何,如果使用传统的逐个端口扫描,效率很低,即使是扫描本机,平均每个端口也需要1秒左右。提高效率的方法有很多如多线程、异步扫描。我采用的是异步扫描方式,其实这种方式也是一种多线程的方式,只是线程是交由系统创建的。
2.程序效果截图
3.源代码
说明:下面代码使用的是原始的Socket,也可以使用C#提供的高级类TcpClient,这样可能做起来会方便点。
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace PortScan
{
class Program
{
static void Main(string[] args)
{