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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With