如何在IPv4的网络中访问IPv6具体实现方案
/**
*
* @param hostname
* @return
* @throws UnknownHostException
*/
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
if (hostname == null) {
throw new UnknownHostException("hostname == null");
} else {
List<InetAddress> inetAddresses = new ArrayList<>();
InetAddress[] host = InetAddress.getAllByName(hostname);
for (InetAddress inetAddress : host
) {
if (inetAddress instanceof Inet4Address) {
inetAddresses.add(0, inetAddress);
} else {
inetAddresses.add(inetAddress);
}
}
return inetAddresses;
}
}
网友评论