views:

358

answers:

2

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?

A: 

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.

Remy Lebeau - TeamB
I looked at getInetAddresses, and it does indeed recognize the IPv6 addresses. So how do I get a ServerSocketChannel that does support IPv6?
Martin v. Löwis
+2  A: 

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.

Martin v. Löwis