简体   繁体   English

在Windows上绑定IPv6服务器套接字

[英]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: 我尝试使用此片段在Windows 7上绑定Java 1.6中的IPv6服务器套接字:

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 不幸的是,这会因IOException而失败:协议族不支持的地址族: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. 我知道Java的编写假设Windows使用单独的v4和v6堆栈(即使Windows 7没有),因此绑定v4和v6的单个套接字也无法工作。 However, this is not what I'm attempting to do: I merely want to bind a v6 socket to the any address (ie ::). 但是,这不是我想要做的:我只想将v6套接字绑定到任何地址(即::)。

Edit : It also fails on Vista. 编辑 :Vista上也失败了。

What am I doing wrong? 我究竟做错了什么?

I found the solution; 我找到了解决方案; it is bug 6230761 . 它是bug 6230761 The only supported way to create an IPv6 server socket channel is to create the serversocket first: 创建IPv6服务器套接字通道的唯一受支持的方法是首先创建serversocket:

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

Edit : this means that NIO cannot really be used with IPv6. 编辑 :这意味着NIO实际上不能与IPv6一起使用。

That error means you are mixing an IPv6 address with a non-IPv6 protocol. 该错误意味着您正在将IPv6地址与非IPv6协议混合使用。 That likely means that the ServerSocketChannel you are starting with does not supports IPv6. 这可能意味着您开始使用的ServerSocketChannel不支持IPv6。 I don't think Java officially supports Windows 7 yet. 我认为Java还没有正式支持Windows 7。 Try using NetworkInterface.getNetworkInterfaces() and NetworkInterface.getInetAddresses() to make sure IPv6 addresses are actually available to your Java app. 尝试使用NetworkInterface.getNetworkInterfaces()和NetworkInterface.getInetAddresses()来确保您的Java应用程序实际可以使用IPv6地址。 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. Java文档甚至说,当IPv6不可用或IPv6已被禁用时尝试传递IPv6地址将引发异常。

I'm seeing this problem with jython as well. 我也看到了jython这个问题。

http://bugs.jython.org/issue1711 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. jython套接字模块必须使用java.nio,因为这是支持cpython兼容性所需的非阻塞功能的唯一方法。

I'm extremely disappointed to see that IPV6 is not supported by the latest java running on the latest windows: this is very poor. 我非常失望地看到最新的Windows上运行的最新java不支持IPV6:这非常糟糕。 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. IPV4地址空间已经耗尽:未来几个月对IPV6支持的需求将会更多,更不用说几年了。

We have a workaround for jython users, which forces the getaddrinfo() function to return IPV4 addresses only. 我们为jython用户提供了一种解决方法,它强制getaddrinfo()函数仅返回IPV4地址。 It's a poor workaround, but at least it gets the users up and running, if they can get an IPV4 address. 这是一个糟糕的解决方法,但至少它可以让用户启动并运行,如果他们可以获得IPV4地址。

http://wiki.python.org/jython/NewSocketModule#IPV6_address_support 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. 记者刚刚在jython跟踪器上发现了另一个关于该bug的报道。 He says that he has had success using java.nio sockets with IPV6 on an early release of JDK 1.7. 他说他在早期版本的JDK 1.7上使用带有IPV6的java.nio套接字取得了成功。

http://bugs.jython.org/issue1711 http://bugs.jython.org/issue1711

So my blog post about IPV6 support on jython wasn't premature :-) 所以我关于jython的IPV6支持的博客文章并不成熟:-)

http://jython.xhaus.com/jython-supports-ipv6/ 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. 有关上下文,请参阅下面的第一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM