我使用了 NSDServiceInfo.getHost()。getHostAddress() 输出的是 IPv4 到 IPv6 的设备和路由器。对于设备,我已经可以使用 IPv4,所以我需要 IPv6。除了路由器,我已经可以了,所以我需要 IPV4 IPV6。
我尝试过的解决方案。但是当他的主机地址发出 IPV4 时,则退出错误“不是 IPv6 地址:[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 ());