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?
To create a socketDeclare an addrinfo object that contains a sockaddr structure and initialize these values. For this application, the Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned. The application requests the socket type to be a stream socket for the TCP protocol.
Technically yes, you can host an IPv6-only website (just like there are IPv4-only websites) – but the issue is that people who only have IPv4 Internet connections won't be able to access it directly.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With