gpt4 book ai didi

sockets - Delphi:TCP打洞

转载 作者:行者123 更新时间:2023-12-03 12:08:58 26 4
gpt4 key购买 nike

我正在尝试在Delphi中进行 TCP孔打洞。我只打开了一个端口-10000(用于测试)。

这是我的代码段(full source code):

procedure TFormMain.btnEnableRedirectClick(Sender: TObject);
begin
IdTCPServerRetr.Active:=True;
end;

procedure TFormMain.btnConnectToClientClick(Sender: TObject);
begin
if IdTCPClientRemote.Port = 0 then
ShowMessage('Wait for connection to redirect server...')
else begin
IdTCPClientRemote.Host:=MyIP;
IdTCPClientRemote.Connect;
ShowMessage('S U C C E S S');
end;
end;

procedure TFormMain.btnConnectClick(Sender: TObject);
begin
IdTCPClientLocal.Host:=MyIP;
IdTCPClientLocal.Connect;
ShowMessage('Connected to redirect server!');
end;

procedure TFormMain.btnListenClick(Sender: TObject);
begin
IdTCPServerLocal.DefaultPort:=IdTCPClientLocal.Socket.Binding.Port;
IdTCPServerLocal.Active:=True;
ShowMessage('Local server started!');
end;

procedure TFormMain.IdTCPServerRetrExecute(AContext: TIdContext);
begin
IdTCPClientRemote.Port:=AContext.Connection.Socket.Binding.PeerPort;
end;

首先,我在端口10000上运行远程服务器(用于重定向)。

IdTCPServerRetr.Active:=True;



之后,我使用与客户端相同的端口连接到重定向服务器(端口10000)并创建本地服务器。

IdTCPClientLocal.Connect; IdTCPServerLocal.DefaultPort:=IdTCPClientLocal.Socket.Binding.Port; IdTCPServerLocal.Active:=True;



(客户端和服务器具有 重用套接字套接字 = rsTrue )

之后,我尝试在客户端的内部端口上连接到客户端,但出现错误“ 连接超时”。

我认为问题在于远程客户端无法找到要连接的服务器,因为客户端已经与重定向服务器建立了连接。

最佳答案

尝试将TIdTCPClient.ConnectTimeout设置为较高的值,例如5000或更大(毫秒)。

关于sockets - Delphi:TCP打洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203663/

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