delphi Sockets检查端口是否开放  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi Sockets检查端口是否开放


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(pansichar(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.



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.039630889892578 seconds