How to Support Both Ipv4 & Ipv6 on Java

How to support both IPv4 & IPv6 on 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

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

Ignite Communication SPI support for IPv6

The problem is that in many networks, IPv6 is enabled but not configured, meaning that Ignite can see the address and tries to use it, but no data is routed.

If your IPv6 network is correctly configured and all the nodes can "see" each other, then it should work.

How to support both IPv4 and IPv6 connections

The best approach is to create an IPv6 server socket that can also accept IPv4 connections. To do so, create a regular IPv6 socket, turn off the socket option IPV6_V6ONLY, bind it to the "any" address, and start receiving. IPv4 addresses will be presented as IPv6 addresses, in the IPv4-mapped format.

The major difference across systems is whether IPV6_V6ONLY is a) available, and b) turned on or off by default. It is turned off by default on Linux (i.e. allowing dual-stack sockets without setsockopt), and is turned on on most other systems.

In addition, the IPv6 stack on Windows XP doesn't support that option. In these cases, you will need to create two separate server sockets, and place them into select or into multiple threads.

IPv4/IPv6 network calculations and validation for Java?

It's only for IPv4, but the SubnetUtils class that is part of Commons Net has the functionality you are looking for. Based on that you could code up an IPv6 version and contribute it back to the project! :)



Related Topics



Leave a reply



Submit