问题描述
除了捕获 netstat 调用之外,是否有更优雅的方法来检查 Delphi 是否可以使用 TCP 端口?
Is there a more elegant way of checking if a TCP port is available with Delphi other than catching a netstat call?
推荐答案
我想你可以使用 Indy 的组件来做到这一点.例如,TIdHTTPServer
会在端口被打开时引发异常.
I guess you can use Indy's components to do that. For instance a TIdHTTPServer
will raise an exception if a port is in use when it is being opened.
所以基本上你可以创建这样的组件,将其绑定到 localhost:<yourport>
并且如果引发异常(捕获并检查它)那么端口可能正在使用中,否则它是免费的.
So basically you could create such component, bind it to localhost:<yourport>
and if an exception is raised ( catch it and check it ) then the port is probably in use, else it is free.
我猜其他 indy 组件可以判断端口是否打开,但我现在无法查看.
I guess other indy components can tell if a port is open or not, but I can't look at it right now.
这只是给你一个方法.
这篇关于Delphi 如何检查 TCP 端口是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!