首页 > 解决方案 > (我认为)Thrift 服务器只监听 ipv6

问题描述

我是 Apache Thrift 的新手,并且正在玩 Apache 在这里提供的玩具 Delphi 客户端服务器示例:https ://thrift.apache.org/tutorial/delphi

我们对名称以及端口和 IP 的设置方式进行了一些小的更改,但其他方面基本相同。

服务器中,我们有以下代码:

    PORT := StrToInt(ParamStr(1));
    handler   := TPhraseGeneratorHandler.Create;
    processor := TPhraseGenerator.TProcessorImpl.Create( handler);
    transport := TServerSocketImpl.Create( PORT);
    server    := TSimpleServer.Create( processor, transport);

    WriteLn( 'Starting the server on port ' + PORT.ToString + '...');
    server.Serve();

客户端中,我们有以下代码:

var transport     : ITransport;
    protocol      : IProtocol;
    client        : TPhraseGenerator.Iface;
    phraseRequest : IPhraseRequest;
// Let the user pass in the parameters for host and port
    HOST : String;
    PORT : Integer;

begin
  try
    // Open a connection to the server using the host and port supplied by the user
    HOST := ParamStr(1);
    PORT := StrToInt(ParamStr(2));
    WriteLn('Openning a connection to the server: ' + HOST + ' on port: ' + PORT.ToString);
    transport := TSocketImpl.Create( HOST, PORT, 10000); // specifically add a timeout as our test server deliberately goes to sleep for 5000ms
    protocol  := TBinaryProtocolImpl.Create( transport);
    client    := TPhraseGenerator.TClient.Create( protocol);
    transport.Open;

如果我们在同一台机器上打开客户端和服务器并使用“localhost”,我们可以让它们进行通信。

但是,如果我们在不同的机器上打开它们并指定服务器的 ipv4 地址,我们就不能。

使用 netstat 我们得到以下信息:

D:\Temp>netstat -ano | findstr 9090
  TCP    [::]:9090              [::]:0                 LISTENING       15368

我认为这表明服务器仅在 ipv6 上侦听。

问:我说的对吗?如果是这样,我们怎样才能让它在 ipv4 上收听?

标签: delphithrift

解决方案


我会说你是 100% 正确的

相关部分来自CreateSocket()

// Pick the ipv6 address first since ipv4 addresses can be mapped
// into ipv6 space.
Res := Result.Res;
while Assigned(Res) do begin
    if (Res^.ai_family = AF_INET6) or (not Assigned(Res^.ai_next)) then
        Break;
    Res := Res^.ai_next;
end;

FSocket := Winapi.Winsock2.socket(Res^.ai_family, Res^.ai_socktype, Res^.ai_protocol);

推荐阅读