【发布时间】:2016-10-05 10:01:05
【问题描述】:
我使用了 NSDServiceInfo.getHost()。 getHostAddress() 输出的是 IPv4 到 IPv6 的设备和路由器。对于设备,我已经可以使用 IPv4,所以我需要 IPv6。和路由器一样,我已经可以了,所以我需要 IPV4 IPV6。
我尝试过的解决方案。但是当他的hostaddress发出IPV4时,则退出错误“Not an IPv6 address: [xx, xx, xx, xx]
同时,当它发出主机地址 IPv6 时,错误消息“java.net.Inet6Address can't be cast to java.net.Inet4Address”
我喜欢这种编码
List <NSDServiceInfo> Data = new ArrayList<>();
InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress());
byte [] addressBytes = hostInet.getAddress();
Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet));
Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes);
Log.d ( "IP", "IPv4" + dest4.getHostAddress ());
Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());
【问题讨论】: