Android and IPv6 in Java

2019-04-20 09:14发布

问题:

I am developing an Android application and I need to use IPv6 to connect to a server.

IPv6 is enabled on the phone and I can see my local IPv6 address with ip addr. I can also successfully ping6 my phone from the PC and viceversa.

But hen I try to get local addresses on Android through the Java command NetworkInterface.getNetworkInterfaces() I only get IPv4 addresses.

I also tryed to open a client socket but the line Socket s = new Socket(MYSERVERIPV6ADDRESS, PORT); always throws java.net.SocketException: Invalid argument. I am sure the address is correct because I tryed with the same code on my computer and works perfectly.

It seems that IPv6 is supported by the operating system but not by the Java virtual machine. Is there a way to solve this problem?

回答1:

Use this static method in Inet6Address to get a Inet6Address object for your address,

Inet6Address getByAddress (String host, byte[] addr, int scope_id)

then use, the following socket constructor to get a socket,

Socket(InetAddress dstAddress, int dstPort).



回答2:

IP6 support is the choice of the vendor to include, to my understanding, support is there.

I am assuming you are testing you app in emulator. Looking at how android does networking, http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking It's behind an IPv4 router addressed 10.0.2.1. Hence, you are not able to open a Socket using IPv6. It's all dependent if a path exists of all the routers who are IPv6 compatible from your phone to your destination.