I\'m implementing a Discover process that:
Some parts of a channel close are deferred to the next select() if the channel is registered with a Selector. It is documented somewhere in the forest of Selector, AbstractSelector, SelectorSpi, SelectableChannel, AbstractSelectableChannel, where I can never find it when I need it. If you're within the select loop and thread when you close the channel, you can make it immediate by calling selectNow().
I did get some errors but the socket get closed properly... which is oki for my needs
No, in case you've got errors your channel is NOT closed properly.
You have to do close
in the finally
clause of your try
block.
Selector selector = Selector.open();
try
{
DatagramChannel channel = DatagramChannel.open();
try
{
channel.configureBlocking(true);
channel.socket().bind(
new InetSocketAddress(InetAddress.getLocalHost(), 9005)
);
SelectionKey clientKey = channel.register(selector, SelectionKey.OP_READ);
clientKey.cancel();
}
finally
{
channel.close();
}
}
finally
{
selector.close( )
}