简体   繁体   中英

IPv6 support in Java

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()

You can use Inet6Address class.

java.net.Inet6Address.getHostAddress()

Refer this question. InetAddress.getHostAddress() ipv6 compliant?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM