Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to support both IPv4 & IPv6 on Java

Tags:

One of our Java program when started, it only listen on 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.

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

like image 454
Howard Avatar asked Apr 30 '12 04:04

Howard


People also ask

Can I use both IPv4 and IPv6?

IPv4 and IPv6 must coexist for some number of years, and their coexistence must be transparent to end users. If an IPv4-to-IPv6 transition is successful, end users should not even notice it. A dual-stack device is a device with network interfaces that can originate and understand both IPv4 and IPv6 packets.

Which two methods would you recommend so that IPv4 and IPv6 can coexist together?

Providing coexistence in network layer routing can be accomplished in any one of three ways: Enabling IPv6 on routers that carry IPv4. Enabling IPv6 on other routers as a parallel network internal to the customer-perceived network.

What communication models are supported by both IPv4 and IPv6?

The MTG model put IPv4 and IPv6 networks as two heterogeneous peers: IPv4-only network on one side and IPv6-only network on the other. The MTG works for IPv4 and IPv6 equally.

Why do I have both IPv4 and IPv6?

IPv6 and IPv4 are different and incompatible systems, you are running a 'dual stack' and your OS will try one then the other - typically 6 and then 4. If a site has a AAAA record, and you have a dual stack setup, you will typically connect to ipv6 first then ipv4.


1 Answers

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

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 and Windows Server 2008 handle dual stacks just fine; Windows XP not so much :)

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

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

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

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:

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
like image 166
paulsm4 Avatar answered Sep 28 '22 05:09

paulsm4