![](/img/trans.png)
[英]How to find the port of a TcpServer started using TcpServer.new(0) in ruby
[英]Connecting to ruby tcpserver from delphi using indy
我得到了以下示例(作者:Matt S)在cmd提升模式下运行: 如何测试“推送通知”反馈服务?
我可以使用“ telnet localhost 2196”连接到该ruby服务器,它将开始向telnet客户端发送数据。
但是,如果我随后尝试使用indy在delphi中使用它:
TcpClient := TIdTcpClient.Create(nil);
TcpClient.Host := 'localhost';
TcpClient.Port := 2196;
TcpClient.Connect;
我收到以下错误:套接字错误#10061一旦调用“连接”,连接就会被拒绝。 好像找不到tcp服务器。
我在这里可能做错了什么?
该错误表示服务器未在127.0.0.1
侦听。 服务器可能正在侦听其他本地IP,而没有侦听127.0.0.1
。
localhost
不一定仅指127.0.0.1
。 通过gethostbyname()
或getaddrinfo()
查找查询可以将localhost
解析为实际的本地IP,然后该telnet
将尝试连接到该本地IP,直到成功为止。
但是Indy当前仅将localhost
视为127.0.0.1
,而没有尝试动态解析它:
function TIdStack.ResolveHost(const AHost: string;
const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string;
begin
Result := '';
case AIPVersion of
Id_IPv4: begin
// Sometimes 95 forgets who localhost is
if TextIsSame(AHost, 'LOCALHOST') then begin {Do not Localize}
Result := '127.0.0.1'; {Do not Localize}
end else if IsIP(AHost) then begin
Result := AHost;
end else begin
Result := HostByName(AHost, Id_IPv4);
end;
end;
Id_IPv6: begin
if TextIsSame(AHost, 'LOCALHOST') then begin {Do not Localize}
Result := '::1'; {Do not Localize}
end else begin
Result := IdGlobal.MakeCanonicalIPv6Address(AHost);
if Result = '' then begin
Result := HostByName(AHost, Id_IPv6);
end;
end;
end;
else begin
IPVersionUnsupported;
end;
end;
end;
即使Indy尝试将localhost
解析为主机名,它也不会尝试连接到所有报告的IP。 那是Indy问题清单上的TODO项目 。
我的建议是使用getaddrinfo()
手动解析localhost
,然后使用循环遍历所报告的IP,重置TIdTCPClient.Host
(可能还有TIdTCPClient.IPVersion
)并为每一个都调用TIdTCPClient.Connect()
,直到成功为止否则循环结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.