想要同时使用 Inet4Address 和 Inet6Address 的 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3853286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java application wanting to use both Inet4Address and Inet6Address at the same time
提问by His
I have a Java application that needs to connect via sockets to two different servers on two separate machines. One server has been configured to listen on IPv4 connections, while the other has been configured to listen on IPv6 connections.
我有一个 Java 应用程序,它需要通过套接字连接到两台不同机器上的两个不同服务器。一台服务器已配置为侦听 IPv4 连接,而另一台已配置为侦听 IPv6 连接。
Now, assuming "host1" is the machine name of the server listening on IPv4 connections, while "host2" is the machine name of the server listening on IPv6 connections. I need to get an Inet4Address
for "host1" and an Inet6Address
for "host2" to create a socket connection to each server, such as the following:
现在,假设“host1”是侦听 IPv4 连接的服务器的机器名称,而“host2”是侦听 IPv6 连接的服务器的机器名称。我需要获取一个Inet4Address
for "host1" 和一个Inet6Address
for "host2" 来创建到每个服务器的套接字连接,如下所示:
Inet4Address inet4 = (Inet4Address)InetAddress.getByName("host1");
InetSocketAddress soc4 = new InetSocketAddress(inet4, 7777);
...
Inet6Address inet6 = (Inet6Address)InetAddress.getByName("host2");
InetSocketAddress soc6 = new InetSocketAddress(inet6, 7777);
...
However, the JVM by default prefers to use IPv4 addresses over IPv6 addresses for backward compatibility reasons. So, in the above code, the first attempt to connect to "host1" succeeds, but the second attempt to connect to "host2" fails because InetAddress.getByName("host2")
returns an Inet4Address
instead of Inet6Address
.
但是,出于向后兼容性的原因,JVM 在默认情况下更喜欢使用 IPv4 地址而不是 IPv6 地址。因此,在上面的代码中,第一次尝试连接到“host1”成功,但第二次尝试连接到“host2”失败,因为InetAddress.getByName("host2")
返回的是一个Inet4Address
而不是Inet6Address
。
I understand that I can set the system property java.net.preferIPv6Addresses
to true to prefer IPv6 addresses over IPv4, but this in turn causes the second attempt to connect to "host2" succeeds, but the first attempt to connect to "host1" failed(!) because InetAddress.getByName("host1")
returns an Inet6Address
instead of Inet4Address
.
我知道我可以将系统属性设置java.net.preferIPv6Addresses
为 true 以更喜欢 IPv6 地址而不是 IPv4,但这反过来会导致第二次尝试连接到“host2”成功,但第一次尝试连接到“host1”失败(!),因为InetAddress.getByName("host1")
返回一个Inet6Address
代替Inet4Address
。
The system property java.net.preferIPv6Addresses
is only being read once (see InetAddress line 212-218) and so it would have no effects even if I change its value back to false after setting it to true.
系统属性java.net.preferIPv6Addresses
仅被读取一次(请参阅 InetAddress 第 212-218 行),因此即使在将其设置为 true 后将其值改回 false 也不会有任何影响。
So what I can I do in this case? This seems like a common problem, so surely there has to be a way already to do it.
那么在这种情况下我能做什么呢?这似乎是一个普遍的问题,所以肯定已经有一种方法可以做到这一点。
Note that I can of course use InetAddress.getByAddress()
and supply each machine's IP address explicitly instead to get back an Inet4Address
and Inet6Address
, but I do not want to do this unless I really have to. So other solutions please.
请注意,我当然可以InetAddress.getByAddress()
明确地使用和提供每台机器的 IP 地址来取回Inet4Address
and Inet6Address
,但我不想这样做,除非我真的必须这样做。所以请其他解决方案。
Oh, I am using java 1.6.0_19 in case it matters.
哦,我正在使用 java 1.6.0_19 以防万一。
Thanks!
谢谢!
采纳答案by Andrew Duffy
static Inet6Address getInet6AddressByName(String host) throws UnknownHostException, SecurityException
{
for(InetAddress addr : InetAddress.getAllByName(host))
{
if(addr instanceof Inet6Address)
return (Inet6Address)addr;
}
throw new UnknownHostException("No IPv6 address found for " + host);
}
回答by Colin Hebert
Unless you have a specific need for methods only available in Inet6Address
or Inet4Address
you shouldn't use these classes directly, instead use InetAddress
.
除非您对仅在 中可用的方法有特定需求,Inet6Address
否则Inet4Address
您不应直接使用这些类,而是使用InetAddress
.
This way you won't need to cast and risk to have a CCE.
这样,您就无需投保并冒险获得 CCE。
In your case I don't really see good reasons to use specifically IPv6 or IPv4 classes.
在您的情况下,我真的没有看到专门使用 IPv6 或 IPv4 类的充分理由。
Remember, IPv6 is compatible with IPv4, so you don't really have to worry when you use an IPv4 address with an IPv6 system.
请记住,IPv6 与 IPv4 兼容,因此当您在 IPv6 系统中使用 IPv4 地址时,您真的不必担心。
Resources :
资源 :
回答by krishna
Have you tried with Inet6Address.getAllByName("host2")
.
你有没有试过Inet6Address.getAllByName("host2")
。
this must return IPv6 addres of host, that can be used to create socket.
这必须返回主机的 IPv6 地址,可用于创建套接字。