5

我正在尝试使用异步通信模型创建服务器并希望绑定多个端口,但是当我调用一个以上的 bind 方法时,它会引发错误“AlreadyBoundException”。有没有办法做到这一点?这是我的代码

try(AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open()){
                if(listener.isOpen()){
                    listener.setOption(StandardSocketOptions.SO_RCVBUF, 4*1024);
                    listener.setOption(StandardSocketOptions.SO_REUSEADDR, true);
                    listener.bind(new InetSocketAddress(9001));
                    listener.bind(new InetSocketAddress(9002));
4

1 回答 1

0

您只能将单个AsynchronousServerSocketChannel(或任何其他NetworkChannelor Socket)绑定到一个端口。如果套接字已经绑定,该bind()方法将抛出AlreadyBoundException

但是,您可以使用多个AsynchronousServerSocketChannel,每个端口一个。

于 2015-02-24T12:46:53.707 回答