如何支持IPv4和& Java上的IPv6 [英] How to support both IPv4 & IPv6 on Java

查看:556
本文介绍了如何支持IPv4和& Java上的IPv6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的一个Java程序启动时,只能监听IPv6(8080)

One of our Java program when started, it only listen on IPv6 (8080)

例如

# 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      -               

问题是它无法从外部访问(localhost除外),为了解决这个问题,我手动添加

The problem is it is not accessible from outside (except localhost), to solve this, I have this manually add

-Djava.net.preferIPv4Stack=true

但是这使得该程序仅适用于IPv4网络。

But this make the program is only for IPv4 network.

是否可以像上面那样执行 sshd 这两种支持IPv4和IPv6?

Is it possible to do something like the sshd as above, both support IPv4 and IPv6?

推荐答案

我怀疑它不像操作系统网络堆栈/操作系统网络配置问题那样是Java编程问题:

I suspect it's less a Java programming issue than an OS networking stack/OS network configuration issue:


http://coding.derkeiler.com/Archive/Java/comp。 lang.java.help/2009-09/msg00087.html

在某些操作系统上,单个本地TCP套接字可以监听两个$ b上的端口$ b IPv4和IPv6同时进行。它能够接受来自
远程IPv4和远程IPv6客户端的连接。在其他操作系统(如WinXP)上,
操作系统原生套接字不能这样做,但只能接受IPv4或
IPv6,而不是两者。在这些操作系统上,必须有两个listen
套接字才能接受来自远程IPv4
和IPv6客户端的连接,一个套接字用于监听IPv4连接,另一个套接字用于
IPv6。

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.

Windows 7和Windows Server 2008处理双栈就好了; Windows XP并没有那么多:)

Windows 7 and Windows Server 2008 handle dual stacks just fine; Windows XP not so much :)

你似乎在Linux上 - 大多数现代Linux桌面和服务器也可以处理双ipv4 ipv6而没有任何问题。

You seem to be on Linux - most modern Linux desktops and servers also handle dual ipv4 ipv6 with no problem.

这是一篇关于互操作性的好文章:

Here's a good article on interoperability:

  • http://ntrg.cs.tcd.ie/undergrad/4ba2.02/ipv6/interop.html

您知道如何为您的Java应用程序关闭IPV6: -Djava.net.preferIPv4Stack = true

You know how you can "turn off" IPV6 for your Java application: -Djava.net.preferIPv4Stack=true

您还可以强制您的服务器使用IPV6,如下所示: echo 0> / proc / sys / net / ipv6 / bindv6only

You can also force your server to use IPV6 like this: echo 0 > /proc/sys/net/ipv6/bindv6only

这可以说是你最好的来源:

This is arguably your best source:

  • http://docs.oracle.com/javase/6/docs/technotes/guides/net/ipv6_guide/index.html

你绝对应该能够完成你想要的(至少在Java编程层面),除非你受到外部网络问题的限制:

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:

Nodes)      V4 Only  V4/V6  V6 Only
            -------  -----  -------
V4 Only     x        x   
V4/V6       x        x      x
V6 Only              x      x

PS:

这是一个更好的链接,它解释了套接字级别发生的事情。它不是Java(它是C),但确切的示例原则适用:

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:

  • Accept connections from both IPv6 and IPv4 clients

这篇关于如何支持IPv4和& Java上的IPv6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆