user2046211
user2046211

Reputation: 376

NIO. Multiple client channels listening on same port

Using NIO, how do I register multiple client connections on the same machine to listen on the same port.

I have something like this for the first channel...

channel1 = DatagramChannel.open();
channel1.configureBlocking(false);
channel1.socket().bind(new InetSocketAddress(localPort));
channel1.connect(socketAddress);
SelectionKey key1 = channel1.register(readSelector, SelectionKey.OP_READ);
key1.attach(driverHandler1);

Now how do I configure channel2 to also listen on the same port since I cant bind() again.

Upvotes: 3

Views: 1237

Answers (1)

user207421
user207421

Reputation: 310957

Before you bind each channel you must call channelN.socket().setReuseAddress(true), for each N. Then you can bind them all.

Upvotes: 1

Related Questions