5

I'm trying to create a server with an asynchronous communication model and want to bind multiple ports, but it throws an error "AlreadyBoundException" when I call one more than bind method. Is there any possible way to do this? Here's my code

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));
한재현
  • 51
  • 2

1 Answers1

0

You can only bind a single AsynchronousServerSocketChannel (or any other NetworkChannel or Socket) to one port. The bind() method throws an AlreadyBoundException if the socket is already bound

You can, however, use with multiple AsynchronousServerSocketChannel, one for each port.

Filipe Borges
  • 2,712
  • 20
  • 32