Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPv6 support in Java

Tags:

java

ipv6

I am trying to test a java program for IPv6 support on my local system having Windows 7 OS. I have assigned an IPv6 address and have disabled IPv4 in Network Connection.

When I execute ipconfig command I get IPv6 address as output.

However, when I execute below java code I get 127.0.0.1 which is an IPv4 address

System.setProperty("java.net.preferIPv6Stack","true");

InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());

As per my understanding the above code should print 0:0:0:0:0:0:0:1.

Have I missed something?

like image 254
Vinod Avatar asked Sep 09 '13 11:09

Vinod


1 Answers

Problem is resolved. I was setting wrong System property. Correct system property that needs to be set is

System.setProperty("java.net.preferIPv6Addresses","true")

After setting this property the IPv6 address assigned in Network Connection will be displayed.

IPv6 loopback address can be retrieved using below code.

InetAddress.getLoopbackAddress().getHostAddress()
like image 124
Vinod Avatar answered Nov 14 '22 22:11

Vinod