gpt4 book ai didi

通过监听端口发现 LAN 上的 MySQL 服务器(Inno Setup)

转载 作者:可可西里 更新时间:2023-11-01 02:36:54 24 4
gpt4 key购买 nike

我需要用Listening Port搜索IP地址来查找局域网中的其他PC(尝试发现MySQL服务器)并得到结果 IP 谁有那个端口监听。

与此代码类似的用于测试套接字但在 Inno Setup 中工作的代码:

program pfinger;

uses sockets,errors;

Var
Addr : TInetSockAddr;
S : Longint;
Sin,Sout : Text;
Line : string;

begin
Addr.sin_family:=AF_INET;
{ port 79 in network order }
Addr.sin_port:=79 shl 8;
{ localhost : 127.0.0.1 in network order }
Addr.sin_addr.s_addr:=((1 shl 24) or 127);
S:=fpSocket(AF_INET,SOCK_STREAM,0);
If Not Connect (S,ADDR,SIN,SOUT) Then
begin
Writeln ('Couldn''t connect to localhost');
Writeln ('Socket error : ',strerror(SocketError));
halt(1);
end;
rewrite (sout);
reset(sin);
writeln (sout,paramstr(1));
flush(sout);
while not eof(sin) do
begin
readln (Sin,line);
writeln (line);
end;
fpShutdown(s,2);
close (sin);
close (sout);
end.

我已经尝试过其他方法(Java 代码)。但是当我需要解压缩 JRE 以运行 Java jar 时,安装程​​序启动缓慢。我尝试包装我的 Java 代码,但不知道在哪里/如何找到类似于 Java 的 Pascal 函数。

最佳答案

要检查服务器是否正在监听端口,您可以使用 Winsock OLE control :

type
TSocketState =
(sckClosed, sckOpen, sckListening, sckConnectionPending, sckResolvingHost,
sckHostResolved, sckConnecting, sckConnected, sckClosing, sckError);

type
TMsg = record
hwnd: HWND;
message: UINT;
wParam: Longint;
lParam: Longint;
time: DWORD;
pt: TPoint;
end;

const
PM_REMOVE = 1;

function PeekMessage(var lpMsg: TMsg; hWnd: HWND; wMsgFilterMin, wMsgFilterMax,
wRemoveMsg: UINT): BOOL; external 'PeekMessageA@user32.dll stdcall';
function TranslateMessage(const lpMsg: TMsg): BOOL;
external 'TranslateMessage@user32.dll stdcall';
function DispatchMessage(const lpMsg: TMsg): Longint;
external 'DispatchMessageA@user32.dll stdcall';

procedure AppProcessMessage;
var
Msg: TMsg;
begin
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;

function CheckPort(Host: string; Port: Integer): Boolean;
var
Socket: Variant;
begin
Socket := CreateOleObject('MSWinsock.Winsock');
Socket.RemoteHost := Host;
Socket.RemotePort := Port;
Socket.Connect;

{ Winsock requires message pumping }
while not (Socket.State in [sckConnected, sckError]) do
begin
AppProcessMessage;
end;

Result := (Socket.State = sckConnected);

if Result then
begin
Log(Format('Port %d on %s is open', [Port, Host]));
end
else
begin
Log(Format('Port %d on %s is NOT open', [Port, Host]));
end;
Socket.Close;
end;

请注意,Winsock 控件需要消息队列泵送。因此,您可能需要在运行检查之前禁用向导,以防止用户弄乱表单。


致谢:AppProcessMessage 来自 How to execute 7zip without blocking the InnoSetup UI?

关于通过监听端口发现 LAN 上的 MySQL 服务器(Inno Setup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com