How to check if a TCP port is available with Delphi?

依然范特西╮ 提交于 2020-01-01 06:07:10

问题


Is there a more elegant way of checking if a TCP port is available with Delphi other than catching a netstat call?


回答1:


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.

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.

I guess other indy components can tell if a port is open or not, but I can't look at it right now.

This was just to give you an approach.




回答2:


@Mattl, if Available means open for you, you can use this code.

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.



回答3:


The following code from Synapse works very well:

uses
  blcksock;

function PortAvailable(Port:STring):boolean;
var
  svr : TTCPBlockSocket;
begin
  svr := TTCPBlockSocket.Create;
  try
    svr.Bind('0.0.0.0',Port);
    svr.Listen;
    result := svr.LastError = 0;
    Svr.CloseSocket;
  finally
    svr.Free;
  end;
end;



回答4:


netstat information can be retrieved by calling the GetTcpTable and GetUdpTable functions in the IP Helper API, or IPHLPAPI.DLL. For more information on calling the IPHLPAPI.DLL from Delphi, check out this Network traffic monitor. There are some wrappers for it too, and it is part of JEDI API Library.

I wrote a Delphi version of NetStat long ago, but have since lost the source code. Those resources should get you started though.



来源:https://stackoverflow.com/questions/2156151/how-to-check-if-a-tcp-port-is-available-with-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!