(Я думаю) Thrift сервер только слушает на ipv6 - PullRequest
0 голосов
/ 18 марта 2020

Я новичок в Apache Thrift и играл с игрушечным Delphi примером клиент-сервера, который Apache предоставляет здесь: 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.

Q: Я прав? и если да, то как мы можем заставить его слушать ipv4?

1 Ответ

1 голос
/ 19 марта 2020

Я бы сказал, что вы на 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);
...