Java application wanting to use both Inet4Address and Inet6Address at the same time

10,998
static Inet6Address getInet6AddressByName(String host) throws UnknownHostException, SecurityException
{
    for(InetAddress addr : InetAddress.getAllByName(host))
    {
        if(addr instanceof Inet6Address)
            return (Inet6Address)addr;
    }
    throw new UnknownHostException("No IPv6 address found for " + host);
}
Share:
10,998

Related videos on Youtube

His
Author by

His

Updated on May 05, 2022

Comments

  • His
    His about 2 years

    I have a Java application that needs to connect via sockets to two different servers on two separate machines. One server has been configured to listen on IPv4 connections, while the other has been configured to listen on IPv6 connections.

    Now, assuming "host1" is the machine name of the server listening on IPv4 connections, while "host2" is the machine name of the server listening on IPv6 connections. I need to get an Inet4Address for "host1" and an Inet6Address for "host2" to create a socket connection to each server, such as the following:

    Inet4Address inet4 = (Inet4Address)InetAddress.getByName("host1");
    InetSocketAddress soc4 = new InetSocketAddress(inet4, 7777);
    ...
    
    Inet6Address inet6 = (Inet6Address)InetAddress.getByName("host2");
    InetSocketAddress soc6 = new InetSocketAddress(inet6, 7777);
    ...
    

    However, the JVM by default prefers to use IPv4 addresses over IPv6 addresses for backward compatibility reasons. So, in the above code, the first attempt to connect to "host1" succeeds, but the second attempt to connect to "host2" fails because InetAddress.getByName("host2") returns an Inet4Address instead of Inet6Address.

    I understand that I can set the system property java.net.preferIPv6Addresses to true to prefer IPv6 addresses over IPv4, but this in turn causes the second attempt to connect to "host2" succeeds, but the first attempt to connect to "host1" failed(!) because InetAddress.getByName("host1") returns an Inet6Address instead of Inet4Address.

    The system property java.net.preferIPv6Addresses is only being read once (see InetAddress line 212-218) and so it would have no effects even if I change its value back to false after setting it to true.

    So what I can I do in this case? This seems like a common problem, so surely there has to be a way already to do it.

    Note that I can of course use InetAddress.getByAddress() and supply each machine's IP address explicitly instead to get back an Inet4Address and Inet6Address, but I do not want to do this unless I really have to. So other solutions please.

    Oh, I am using java 1.6.0_19 in case it matters.

    Thanks!

  • His
    His over 13 years
    I do. As I said, the two servers are listening to IPv4 and IPv6 connections respectively. The casting I did in the sample code above was just to show that I wanted (and I need to) get an Inet6Address which I then need to pass to InetSocketAddress to connect to "host2" (passing an Inet4Address will cause a connection failure). So, even though I just use the superclass InetAddress all the time, underneath it is actually either Inet4Address or Inet6Address, isn't it? And in this case, I wanted to make it obvious the problem I am having by doing the casting explicitly.
  • Colin Hebert
    Colin Hebert over 13 years
    Yes, it will still Inet4Address only or Inet6Address, and what is the problem with it? IPv6 is compatible with IPv4 there shouldn't be any problem.
  • His
    His over 13 years
    Well, I'm not sure, because that's not what I experienced. I can't use an Inet6Address (or InetAddress if you like) to connect to the server listening on IPv4 connection - I get a connection failure. This: new InetSocketAddress(InetAddress.getByName("host1"), 7777) with InetAddress.getByName("host1") returning an Inet6Address/InetAddress, won't work. But if InetAddress.getByName("host1") returns an Inet4Address/InetAddress, it would work.
  • Colin Hebert
    Colin Hebert over 13 years
    I don't really see the problem here, but after looking at this article : download.oracle.com/javase/1.4.2/docs/guide/net/ipv6_guide It seems that it should work without any cast or whatsoever
  • His
    His over 13 years
    I don't know if it is my machine that is wrong, but I still couldn't use InetAddress (which is Inet6Address underneath) to connect to the server listening on IPv4 connections. In case it matters, the client and server machines are all Windows XP.
  • mpontillo
    mpontillo about 13 years
    -1, this answer is misleading. IPv6 is not compatible with IPv4 in every way. You have to think of them as separate stacks that can't talk to each other. Sometimes, someone might wish to get an address of a specific type. See here (IPv4-compatible addresses deprecated) and current transition mechanisms.