【发布时间】:2011-03-07 07:29:56
【问题描述】:
最近在这个问题上来来回回。尝试使用 Putty 将 SSH 隧道从我机器上的 localhost 端口连接到 Internet 可访问 SSH 服务器另一侧的内部端口。 Putty 不检查端口是否可用。在我打开 Putty 连接之前,我想验证端口是否空闲。
如果端口可用,它可以正常工作,但我需要一种可靠的方法来保证在我在代码中检查端口时端口没有打开。我使用了各种方法,结果很糟糕。我目前正在使用以下代码:
bool IsBusy(int port)
{
IPGlobalProperties ipGP = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endpoints = ipGP.GetActiveTcpListeners();
if (endpoints == null || endpoints.Length == 0) return false;
for (int i = 0; i < endpoints.Length; i++)
if (endpoints[i].Port == port)
return true;
return false;
}
我在计算机上安装了 uTorrent。当我尝试使用端口 3390 时,它失败了,因为 uTorrent 正在通过 TCP 监听该端口。有没有办法确保在给定端口上根本不使用 TCP?
【问题讨论】: