Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding an IPv6 server socket on Windows

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?

like image 479
Martin v. Löwis Avatar asked Aug 31 '09 11:08

Martin v. Löwis


People also ask

How do I create a socket in Windows?

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.

Can you host a server with IPv6?

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.


1 Answers

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.

like image 51
Martin v. Löwis Avatar answered Sep 29 '22 05:09

Martin v. Löwis