Java application wanting to use both Inet4Address and Inet6Address at the same time
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);
}
Related videos on Youtube
His
Updated on May 05, 2022Comments
-
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 anInet6Address
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 anInet4Address
instead ofInet6Address
.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(!) becauseInetAddress.getByName("host1")
returns anInet6Address
instead ofInet4Address
.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 anInet4Address
andInet6Address
, 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 over 13 yearsI 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 toInetSocketAddress
to connect to "host2" (passing anInet4Address
will cause a connection failure). So, even though I just use the superclassInetAddress
all the time, underneath it is actually eitherInet4Address
orInet6Address
, 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 over 13 yearsYes, it will still
Inet4Address
only orInet6Address
, and what is the problem with it? IPv6 is compatible with IPv4 there shouldn't be any problem. -
His over 13 yearsWell, I'm not sure, because that's not what I experienced. I can't use an
Inet6Address
(orInetAddress
if you like) to connect to the server listening on IPv4 connection - I get a connection failure. This:new InetSocketAddress(InetAddress.getByName("host1"), 7777)
withInetAddress.getByName("host1")
returning an Inet6Address/InetAddress, won't work. But ifInetAddress.getByName("host1")
returns anInet4Address/InetAddress
, it would work. -
Colin Hebert over 13 yearsI 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 over 13 yearsI 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 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.