Linux 如何在 Java 上同时支持 IPv4 和 IPv6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10378471/
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
How to support both IPv4 & IPv6 on Java
提问by Howard
One of our Java program when started, it only listen on IPv6 (8080)
我们的 Java 程序之一在启动时只侦听 IPv6 (8080)
e.g.
例如
# netstat -ntpl
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp6 0 0 :::8080 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
The problem is it is not accessible from outside (except localhost), to solve this, I have this manually add
问题是它不能从外部访问(本地主机除外),为了解决这个问题,我手动添加
-Djava.net.preferIPv4Stack=true
But this make the program is only for IPv4 network.
但这使得该程序仅适用于 IPv4 网络。
Is it possible to do something like the sshdas above, both support IPv4 and IPv6?
是否可以做类似上面的sshd 之类的事情,两者都支持 IPv4 和 IPv6?
采纳答案by paulsm4
I suspect it's less a Java programming issue than an OS networking stack/OS network configuration issue:
我怀疑这不是一个 Java 编程问题,而是一个操作系统网络堆栈/操作系统网络配置问题:
http://coding.derkeiler.com/Archive/Java/comp.lang.java.help/2009-09/msg00087.html
On some OSes, a single native TCP socket can listen to a port on both IPv4 and IPv6 simultaneously. It is able to accept connections from remote IPv4 and from remote IPv6 clients. On other OSes (such as WinXP) an OS native socket CANNOT do that, but can only accept from IPv4 or IPv6, not both. On those OSes, it is necessary to have two listen sockets in order to be able to accept connections from both remote IPv4 and IPv6 clients, one socket to listen for IPv4 connections and one for IPv6.
http://coding.derkeiler.com/Archive/Java/comp.lang.java.help/2009-09/msg00087.html
在某些操作系统上,单个本机 TCP 套接字可以同时侦听 IPv4 和 IPv6 上的端口。它能够接受来自远程 IPv4 和远程 IPv6 客户端的连接。在其他操作系统(如 WinXP)上,操作系统本机套接字不能这样做,但只能接受 IPv4 或 IPv6,不能同时接受。在这些操作系统上,必须有两个侦听套接字才能接受来自远程 IPv4 和 IPv6 客户端的连接,一个用于侦听 IPv4 连接的套接字和一个用于侦听 IPv6 的套接字。
Windows 7 and Windows Server 2008 handle dual stacks just fine; Windows XP not so much :)
Windows 7 和 Windows Server 2008 可以很好地处理双栈;Windows XP 没有那么多:)
You seem to be on Linux - most modern Linux desktops and servers also handle dual ipv4 ipv6 with no problem.
您似乎在使用 Linux - 大多数现代 Linux 台式机和服务器也可以毫无问题地处理双 ipv4 ipv6。
Here's a good article on interoperability:
这是一篇关于互操作性的好文章:
You know how you can "turn off" IPV6 for your Java application: -Djava.net.preferIPv4Stack=true
您知道如何为 Java 应用程序“关闭”IPV6: -Djava.net.preferIPv4Stack=true
You can also force your server to use IPV6 like this: echo 0 > /proc/sys/net/ipv6/bindv6only
您还可以强制您的服务器使用 IPV6,如下所示: echo 0 > /proc/sys/net/ipv6/bindv6only
This is arguably your best source:
这可以说是你最好的来源:
You should absolutely be able to accomplish what you want (at least at the Java programming level), unless you're limited by external network issues:
您应该绝对能够完成您想要的(至少在 Java 编程级别),除非您受到外部网络问题的限制:
Nodes) V4 Only V4/V6 V6 Only
------- ----- -------
V4 Only x x
V4/V6 x x x
V6 Only x x
PS:
PS:
Here's one more good link, which explains what's happening at the socket level. It's not Java (it's C), but exactly the sample principles apply:
这是另一个很好的链接,它解释了套接字级别发生的事情。它不是 Java(它是 C),但正是示例原则适用: