我正在创建一个小工具,让我可以检查给定 IP 和端口号的端口状态,如下所示:
private void ScanPort(IPAddress address, int port)
{
using (TcpClient client = new TcpClient())
{
try
{
client.Connect(address, port);
txtDisplay.AppendText("Port: " + nudFrom.Value.ToString() +
" is open." + Environment.NewLine);
}
catch (SocketException)
{
txtDisplay.AppendText("Port: " + nudFrom.Value.ToString() +
" is closed." + Environment.NewLine);
}
}
}
问题是,当端口打开时,检查大约需要一秒钟,如果端口关闭,则该过程大约需要 20 秒时间。
如何更快地检查打开/关闭的端口?