gpt4 book ai didi

java - 在 Java 中返回 IPv6

转载 作者:搜寻专家 更新时间:2023-11-01 03:10:22 29 4
gpt4 key购买 nike

Java 中有没有办法告诉它只返回 IPv6?我已经尝试了所有方法,但无法正常工作。

try
{
InetAddress inet = InetAddress.getByName(hostName);

boolean status = inet.isReachable(5000);

if (status)
{
System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());
}
else
{
System.out.println(inet.getCanonicalHostName() + " Host Unreachable");
}

}
catch (UnknownHostException e)
{
System.err.println("Host does not exists");
}
catch (IOException e)
{
System.err.println("Error in reaching the Host");
}

我用来尝试仅返回 IPv6 的行:

System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());

这会不断返回 IPv4。有人知道为什么要这样做吗?

最佳答案

java.net.Inet6Address 不会覆盖 getByName()
所以它总是会返回特定的 IPv4 地址,除非您的参数本身是有效的 IPv6 地址形式,在这种情况下,此方法将返回一个 Inet6Address 对象。

例如:
getByName("stackoverflow.com") --> Inet4Address
getByName("2001:0db8:85a3:08d3:1319:8a2e:0370:7344") --> Inet6Address

InetAddress.getByName()-文档

Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

> For host specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted.<

因此,如果您想获得 IPv6 地址,您需要在参数中定义它,或者配置 DNS 服务器以返回 IPv6 地址而不是 IPv4 地址。

另一种检索 IPv6 地址的方法是使用 InetAddress.getAllByName("www.google.at"),它会返回主机的所有已知 IP 地址。

例如,您可以使用此方法来过滤返回的数组,该数组返回第一个 IPv6-Address 或 null 如果主机没有:

public Inet6Address getIPv6Addresses(InetAddress[] addresses) {
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
return (Inet6Address) addr;
}
}
return null;
}

更新:对于更多功能,尤其是那些影响 DNS 服务器的功能,我建议使用外部库 DNSJava,因为 DNS 支持的普通 Java 实现很差。
http://www.dnsjava.org/

当前代码:

public class Ping 
{
public void pingHost (String hostName)
{
try
{
InetAddress[] inet = InetAddress.getAllByName(hostName);

String address = this.getIPv4Addresses(inet).getHostAddress();

boolean status = this.getIPv6Addresses(inet).isReachable(5000);

if (status)
{

System.out.println(reverseDns(address) + " Host Reached\t" + this.getIPv6Addresses(inet).getHostAddress());
}
else
{
System.out.println(this.getIPv6Addresses(inet).getCanonicalHostName() + " Host Unreachable");
}

}
catch (UnknownHostException e)
{
System.err.println("Host does not exists");
}
catch (IOException e)
{
System.err.println("Error in reaching the Host");
}
}

public Inet6Address getIPv6Addresses(InetAddress[] addresses)
{
for (InetAddress addr : addresses)
{
if (addr instanceof Inet6Address)
{
return (Inet6Address) addr;
}
}
return null;
}

public Inet4Address getIPv4Addresses(InetAddress[] addresses)
{
for (InetAddress addr : addresses)
{
if (addr instanceof Inet4Address)
{
return (Inet4Address) addr;
}
}
return null;
}

public static String reverseDns(String hostIp) throws IOException
{
Resolver res = new ExtendedResolver();

Name name = ReverseMap.fromAddress(hostIp);
int type = Type.PTR;
int dclass = DClass.IN;
Record rec = Record.newRecord(name, type, dclass);
Message query = Message.newQuery(rec);
Message response = res.send(query);

Record[] answers = response.getSectionArray(Section.ANSWER);
if (answers.length == 0)
return hostIp;
else
return answers[0].rdataToString();
}

}

关于java - 在 Java 中返回 IPv6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11974232/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com