有个小需求在c#项目中要增加一个对某个IP的端口扫描程序,效率要高,目前在网上找的效率都不是太高,于是就问了一下chatgpt ,一共问了三次,只要问的方法对了应该就可以得到答案了,目前看只要是前人写过的代码,都可以回答出来;)
1,帮我用c#写一段快速端口扫描的程序
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PortScanner
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入要扫描的IP地址:");
string ipAddress = Console.ReadLine();
Console.Write("请输入要扫描的起始端口号:");
int startPort = int.Parse(Console.ReadLine());
Console.Write("请输入要扫描的结束端口号:");
int endPort = int.Parse(Console.ReadLine());
Console.WriteLine($"开始扫描IP地址为 {ipAddress} 的主机的端口...");
for (int port = startPort; port <= endPort; port++)
{
Thread thread = new Thread(() => ScanPort(ipAddress, port));
thread.Start();
}
}
static void ScanPort(string ipAddress, int port)
{
using (TcpClient tcpClient = new TcpClient())
{
try
{
tcpClient.Connect(ipAddress, port);
Console.WriteLine($"端口 {port} 开放");
}
catch
{
Console.WriteLine($"端口 {port} 关闭");
}
}
}
}
}
程序会提示用户输入要扫描的IP地址、起始端口号和结束端口号,然后启动多个线程并发地扫描指定IP地址的端口。当扫描到开放的端口时,程序会输出端口号,并标记为开放;否则,程序会输出端口号,并标记为关闭。
这个代码使用的tcpclient的效率不高,要用socket实现效率更高,于是提出问题2
2, 帮我用c#写一段快速端口扫描的程序,用socket实现
using System;
using System.Net;
using System.Net.Sockets;
namespace PortScanner
{
class Program
{
static void Main(string[] args)
{
string target = args[0];
int startPort = int.Parse(args[1]);
int endPort = int.Parse(args[2]);
for (int port = startPort; port <= endPort; port++)
{
try
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Parse(target), port));
Console.WriteLine($"Port {port} is open.");
socket.Close();
}
catch (SocketException)
{
// Port is closed
}
}
}
}
}
该程序使用了 Socket
类来建立一个 TCP 连接,并检查指定 IP 地址上的端口是否打开。程序需要传入三个参数:目标 IP 地址,起始端口和结束端口。程序会循环扫描从起始端口到结束端口的所有端口,并输出开放的端口。如果端口关闭,则会忽略它并继续扫描下一个端口。
上面的代码使用了socket ,但是没有使用多线程处理,于是提出问题3
3帮我用c#写一段多线程的快速端口扫描的程序,用socket实现
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class PortScanner
{
static int startPort = 1;
static int endPort = 65535;
static string host = "localhost";
static void Main(string[] args)
{
// 使用默认参数或命令行参数
if (args.Length == 2)
{
host = args[0];
startPort = int.Parse(args[1]);
}
else if (args.Length == 3)
{
host = args[0];
startPort = int.Parse(args[1]);
endPort = int.Parse(args[2]);
}
Console.WriteLine("Scanning host {0} from port {1} to {2}", host, startPort, endPort);
// 创建线程池
int threads = Environment.ProcessorCount * 4;
ThreadPool.SetMinThreads(threads, threads);
// 开始端口扫描
for (int port = startPort; port <= endPort; port++)
{
ThreadPool.QueueUserWorkItem(CheckPort, port);
}
Console.ReadLine();
}
static void CheckPort(object portObject)
{
int port = (int)portObject;
try
{
// 创建Socket
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
// 设置连接超时时间为1秒
socket.ReceiveTimeout = 1000;
socket.SendTimeout = 1000;
// 连接到端口
socket.Connect(host, port);
// 如果连接成功,输出端口号
Console.WriteLine("Port {0} is open", port);
// 关闭Socket
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
catch (Exception)
{
// 忽略连接失败的情况
}
}
}
该程序使用线程池实现了多线程扫描,可同时扫描多个端口,提高了扫描效率。注意,该程序并未实现超时控制和高速扫描策略,如果需要进行真正的快速端口扫描,请使用专业的扫描工具。
个人评价:问题3的回答使用了socket 线程池,效率就很高了,基本满足我的需求,在此框架下改进即可。看样只要提好需求让chatgpt理解了,写代码就不难了!