C# 检测本地主机端口使用情况
预先感谢您的建议。
我目前正在开发一个程序,该程序使用 Putty 与服务器创建 SSH 连接,该服务器使用本地端口转发来使运行我的软件的客户端能够通过本地主机访问 SSH 服务器后面的服务。
IE: 客户端:20100 -->互联网->通过路由器/防火墙暴露的远程 SSH 服务器 ->本地内联网 ->内联网 Web POP3 服务器:110。
Cmd Line: "putty -ssh -2 -P 22 -C -L 20100:intranteIP:110 -pw sshpassword sshusername@sshserver"
客户端将使用 putty 与 SSH 服务器创建 SSH 连接,并在连接字符串中指定它想要的连接将 Intranet POP3 服务器的端口 110 连接到客户端系统上的端口 20100。因此,客户端将能够打开 localhost:20100 的邮件客户端,并通过 SSH 隧道与内部 POP3 服务器交互。以上是一般描述。我已经知道我正在尝试做的事情会毫无问题地进行,所以我不寻求对上述内容进行辩论。
问题是这样的...我如何确保本地主机上的本地端口(我不能使用动态端口,因此它必须是静态的)不被任何其他应用程序使用或监听?
我当前正在我的 C# 应用程序中执行此代码:
private bool checkPort(int port)
{
try
{
//Create a socket on the current IPv4 address
Socket TestSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Create an IP end point
IPEndPoint localIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
// Bind that port
TestSocket.Bind(localIP);
// Cleanup
TestSocket.Close();
return false;
}
catch (Exception e)
{
// Exception occurred. Port is already bound.
return true;
}
}
我当前正在从 for 循环中的特定端口开始调用此函数,以在第一个可用端口处返回“false”。我尝试的第一个端口实际上是由 uTorrent 监听的。上面的代码没有捕获到这个并且我的连接失败。
确保端口真正免费的最佳方法是什么?我确实知道其他一些程序可能会在我测试期间/之后抢占端口。我只需要找到一些东西来确保在执行测试时它当前根本不被使用。
如果有一种方法可以在测试期间真正保留本地主机端口,我很想听听。
In advance, thank you for your advice.
I am currently working on a program which uses Putty to create a SSH connection with a server that uses local port forwarding to enable a client, running my software, to access the service behind the SSH server via localhost.
IE: client:20100 -> Internet -> Remote SSH server exposed via router/firewall -> Local Intranet -> Intranet Web POP3 Server:110.
Cmd Line: "putty -ssh -2 -P 22 -C -L 20100:intranteIP:110 -pw sshpassword sshusername@sshserver"
Client would use putty to create a SSH connection with the SSH server specifying in the connection string that it would like to tie port 110 of the Intranet POP3 Server to port 20100 on the client system. Therefore the client would be able to open up a mail client to localhost:20100 and interact with the Internal POP3 server over the SSH tunnel. The above is a general description. I already know what I am trying to do will work without a problem so am not looking for debate on the above.
The question is this...How can I ensure the local port (I cannot use dynamic ports, so it must be static) on localhost is not being used or listened to by any other application?
I am currently executing this code in my C# app:
private bool checkPort(int port)
{
try
{
//Create a socket on the current IPv4 address
Socket TestSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Create an IP end point
IPEndPoint localIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
// Bind that port
TestSocket.Bind(localIP);
// Cleanup
TestSocket.Close();
return false;
}
catch (Exception e)
{
// Exception occurred. Port is already bound.
return true;
}
}
I am currently calling this function starting with a specific port in a for loop to get the 'false' return at the first available port. The first port I try is actually being listened to by uTorrent. The above code does not catch this and my connection fails.
What is the best method to ensure a port is truly free? I do understand some other program may grab the port during/after I have tested it. I just need to find something that will ensure it is not currently in use AT ALL when the test is executed.
If there is a way to truly reserve the localhost port during the test, I would love to hear about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是答案检查本地端口是否空闲。
我会推荐这种方式:
Here's the answer how to check if local port is free.
I would recommend this way: