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

3

6

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!

Paradiddle answered 4/10, 2010 at 7:18 Comment(0)
D
6
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);
}
Damian answered 6/10, 2010 at 12:3 Comment(0)
M
0

Have you tried with Inet6Address.getAllByName("host2").

this must return IPv6 addres of host, that can be used to create socket.

Mathew answered 6/10, 2010 at 6:44 Comment(1)
Inet6Address doesn't have a specific implementation of getAllByName(String), so calling Inet6Address.getAllByName(String) is really the same as calling the parent's static method InetAddress.getAllByName(String).Paradiddle
M
-1

Unless you have a specific need for methods only available in Inet6Address or Inet4Address you shouldn't use these classes directly, instead use InetAddress.

This way you won't need to cast and risk to have a CCE.

In your case I don't really see good reasons to use specifically IPv6 or IPv4 classes.

Remember, IPv6 is compatible with IPv4, so you don't really have to worry when you use an IPv4 address with an IPv6 system.


Resources :

Martinic answered 4/10, 2010 at 9:20 Comment(6)
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.Paradiddle
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.Martinic
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.Paradiddle
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 whatsoeverMartinic
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.Paradiddle
-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.Smallman

© 2022 - 2024 — McMap. All rights reserved.