Search code examples
delphi-7ipv6indy10

Indy10 problem connecting to TCP Server using a host name pointing to an IPv6 address


I've put the following in the Windows HOSTS file:

fe80::a5c6:d354:8e27:9f79%16 ip6test

I can ping ip6test in Windows, and I can connect to a server via TIdTCPClient with the IPv6 address above in square brackets, but trying to connect using the host name [ip6test] using the TCP Client instantly fails with a socket error 11001.

Any ideas why?


Solution

  • The bracket workaround only works in TIdHTTP. Don't use it with TIdTCPClient.

    For TIdTCPClient, you have to set its IPVersion property to Id_IPv6 when connecting to an IPv6 server. It is not yet smart enough to detect which IP version an IP address is using, or which IP version(s) a hostname resolves to, so you have to tell it up front (this will be addressed in a future release).

    For example, a simple workaround would look like this:

    Client.Host := ...; 
    Client.Port := ...;
    
    if (Client.Host is an IPv4 address) then
    begin
      Client.IPVersion := Id_IPv4;
      Client.Connect;
    end
    else if (Client.Host is an IPv6 address) then
    begin
      Client.IPVersion := Id_IPv6;
      Client.Connect;
    end
    else
    begin
      try
        // try to connect with IPv6 first...
        Client.IPVersion := Id_IPv6;
        Client.Connect;
      except
        // try again with IPv4...
        Client.IPVersion := Id_IPv4;
        Client.Connect;
      end;
    
      // alternatively, you can use TIdDNSResolver
      // beforehand to resolve the hostname
      // yourself, and then setup TIdTCPClient
      // accordingly... 
    end;