using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PortScanner
{
class Program
{
static void Main(string[] args)
{
// 设置扫描参数
string host = "localhost";
int startPort = 1;
int endPort = 65535;
int numThreads = 1000;
// 创建线程池并开始扫描
ThreadPool.SetMinThreads(numThreads, numThreads);
List<WaitHandle> handles = new List<WaitHandle>();
for (int port = startPort; port <= endPort; port++)
{
handles.Add(new ManualResetEvent(false));
ThreadPool.QueueUserWorkItem(CheckPort, new object[] { host, port, handles.Last() });
}
// 等待所有线程完成
WaitHandle.WaitAll(handles.ToArray());
Console.WriteLine("Scanning complete.");
}
static void