Java中的Android和IPv6 [英] Android and IPv6 in Java

查看:1128
本文介绍了Java中的Android和IPv6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序,我需要使用IPv6连接到服务器。

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

手机上启用了IPv6,我可以看到我的本地IPv6地址使用 ip addr 。我也可以从PC上成功 ping6 我的手机,反之亦然。

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.

但是,我试着获取本地地址Android通过Java命令 NetworkInterface.getNetworkInterfaces()我只获取IPv4地址。

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

我也尝试打开一个客户端套接字但行
套接字s =新套接字(MYSERVERIPV6ADDRESS,PORT);
总是抛出 java.net.SocketException :无效的参数
我确信地址是正确的,因为我在我的计算机上尝试使用相同的代码并且工作正常。

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.

操作系统似乎支持IPv6但不是Java虚拟机。有没有办法解决这个问题?

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?

推荐答案

在Inet6Address中使用这个静态方法来获取你的地址的Inet6Address对象,

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

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

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

然后使用,下面的socket构造函数获取套接字,

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

套接字(InetAddress dstAddress,int dstPort)。

Socket(InetAddress dstAddress, int dstPort).

这篇关于Java中的Android和IPv6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆