Delphi Winsocks检测某个TCP端口师傅正在被使用


本文整理自网络,侵删。

 
//Checking if a TCP port is Open using Delphi and Winsocks
uses
  Winsock;
 
function PortTCP_IsOpen(dwPort : Word; ipAddressStr:AnsiString) : boolean;
var
  client : sockaddr_in;
  sock   : Integer;
 
  ret    : Integer;
  wsdata : WSAData;
begin
 Result:=False;
 ret := WSAStartup($0002, wsdata); //initiates use of the Winsock DLL
  if ret<>0 then exit;
  try
    client.sin_family      := AF_INET;  //Set the protocol to use , in this case (IPv4)
    client.sin_port        := htons(dwPort); //convert to TCP/IP network byte order (big-endian)
    client.sin_addr.s_addr := inet_addr(PAnsiChar(ipAddressStr));  //convert to IN_ADDR  structure
    sock  :=socket(AF_INET, SOCK_STREAM, 0);    //creates a socket
    Result:=connect(sock,client,SizeOf(client))=0;  //establishes a connection to a specified socket
  finally
  WSACleanup;
  end;
end;

begin
 if PortTCP_IsOpen(3306,'127.0.0.1') then
  DoMyStuff();
end;

相关阅读 >>

Delphi转换长文件名函数 getshortname

Delphi asc转换成unicode

Delphi 取得当前监视器的分辨率

Delphi listview中加载图片

Delphi fdconnection1 获取数据库总记录数

Delphi xe10实现移动端支付宝、微信支付接口

Delphi tidhttp 超时设置无效的解决方法

Delphi执行cmd命令

Delphi 通过api 隐藏任务栏所有托盘图标

encryptit.pas

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...