我想你可以使用Indy的组件来做到这一点.例如,TIdHTTPServer如果端口在打开时正在使用,则会引发异常.
所以基本上你可以创建这样的组件,绑定它localhost:<yourport>,如果引发异常(捕获它并检查它),那么端口可能正在使用,否则它是免费的.
我猜其他indy组件可以判断端口是否打开,但我现在无法查看它.
这只是为了给你一个方法.
@Mattl,如果可用意味着对您开放,您可以使用此代码.
program CheckTCP_PortOpen;
{$APPTYPE CONSOLE}
uses
Winsock; //Windows Sockets API Unit
function PortTCPIsOpen(dwPort : Word; ipAddressStr:string) : boolean;
var
client : sockaddr_in;//sockaddr_in is used by Windows Sockets to specify a local or remote endpoint address
sock : Integer;
begin
client.sin_family := AF_INET;
client.sin_port := htons(dwPort);//htons converts a u_short from host to TCP/IP network byte order.
client.sin_addr.s_addr := inet_addr(PChar(ipAddressStr)); //the inet_addr function converts a string containing an IPv4 dotted-decimal address into a proper address for the IN_ADDR structure.
sock :=socket(AF_INET, SOCK_STREAM, 0);//The socket function creates a socket
Result:=connect(sock,client,SizeOf(client))=0;//establishes a connection to a specified socket.
end;
var
ret : Integer;
wsdata : WSAData;
begin
Writeln('Init WinSock');
ret := WSAStartup($0002, wsdata);//initiates use of the Winsock
if ret<>0 then exit;
try
Writeln('Description : '+wsData.szDescription);
Writeln('Status : '+wsData.szSystemStatus);
if PortTCPIsOpen(80,'127.0.0.1') then
Writeln('Open')
else
Writeln('Close');
finally
WSACleanup; //terminates use of the Winsock
end;
Readln;
end.
Run Code Online (Sandbox Code Playgroud)
可以通过调用IP 帮助程序 API 或 IPHLPAPI.DLL 中的GetTcpTable和GetUdpTable函数来检索netstat信息。有关从 Delphi 调用 IPHLPAPI.DLL 的更多信息,请查看此网络流量监视器。它也有一些包装器,它是JEDI API 库的一部分。
我很久以前写了一个 Delphi 版本的 NetStat,但后来丢失了源代码。不过,这些资源应该可以帮助您入门。
| 归档时间: |
|
| 查看次数: |
16590 次 |
| 最近记录: |