function PortOccupied(const APort:Integer):Boolean; //Delphi中判断TCP端口是否被占用
var
S:TSocket;
WSD :TWSAData;
SockAddrIn:TSockAddrIn;
begin
Result:=False;
if (WSAStartup(MAKEWORD(2,2),WSD)= 0) then
begin
S := Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
try
if (S <> SOCKET_ERROR) then
begin
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_addr.S_addr := htonl(INADDR_ANY);
//SockAddrIn.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(IP)));
SockAddrIn.sin_port:= htons(APort);
if (Bind(S,SockAddrIn,SizeOf(SockAddrIn))<>0) then
if (WSAGetLastError = WSAEADDRINUSE) then
Result:=True;
end;
finally
CloseSocket(S);
WSACleanup();
end;
end;
end;
Delphi中判断TCP端口是否被占用
最新推荐文章于 2023-08-30 23:03:29 发布