Question

I try to bind an IPv6 server socket in Java 1.6 on Windows 7, using this fragment:

ssock = ServerSocketChannel.open();
ServerSocket sock = ssock.socket(); 
sock.bind(new InetSocketAddress(InetAddress.getByAddress(new byte[16]), 0));

Unfortunately, this fails with an IOException: Address family not supported by protocol family: bind

I understand that Java is written with the assumption that Windows uses separate v4 and v6 stacks (even though Windows 7 doesn't), and that therefore binding a single socket for both v4 and v6 cannot work. However, this is not what I'm attempting to do: I merely want to bind a v6 socket to the any address (i.e. ::).

Edit: It also fails on Vista.

What am I doing wrong?

Was it helpful?

Solution

I found the solution; it is bug 6230761. The only supported way to create an IPv6 server socket channel is to create the serversocket first:

ServerSocket s = new ServerSocket();
s.bind(new InetSocketAddress(InetAddress.getByName("::"), 0));

Edit: this means that NIO cannot really be used with IPv6.

OTHER TIPS

That error means you are mixing an IPv6 address with a non-IPv6 protocol. That likely means that the ServerSocketChannel you are starting with does not supports IPv6. I don't think Java officially supports Windows 7 yet. Try using NetworkInterface.getNetworkInterfaces() and NetworkInterface.getInetAddresses() to make sure IPv6 addresses are actually available to your Java app. The Java docs even say that trying to pass an IPv6 address when IPv6 is not available, or when IPv6 has been disabled, will raise exceptions.

I'm seeing this problem with jython as well.

http://bugs.jython.org/issue1711

The jython socket module must use java.nio, because that is the only way to support the non-blocking functionality that cpython compatibility requires.

I'm extremely disappointed to see that IPV6 is not supported by the latest java running on the latest windows: this is very poor. The IPV4 address space is exhausted already: there's going to be a lot more demand for IPV6 support in the coming months, let alone years.

We have a workaround for jython users, which forces the getaddrinfo() function to return IPV4 addresses only. It's a poor workaround, but at least it gets the users up and running, if they can get an IPV4 address.

http://wiki.python.org/jython/NewSocketModule#IPV6_address_support

There's just been another report from the reporter of that bug on the jython tracker. He says that he has had success using java.nio sockets with IPV6 on an early release of JDK 1.7.

http://bugs.jython.org/issue1711

So my blog post about IPV6 support on jython wasn't premature :-)

http://jython.xhaus.com/jython-supports-ipv6/

Hmmm, it appears that my second answer has been listed above my first answer. See for my first answer below for context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top