I written simple NIO server:
public static void main(String[] args) throws Exception
{
ByteBuffer buffer = ByteBuffer.allocateDirect(65536);
Selector selector = Selector.open();
ServerSocketChannel server = ServerSocketChannel.open();
server.configureBlocking(false);
server.bind(new InetSocketAddress(724));
server.register(selector, SelectionKey.OP_ACCEPT);
while(server.isOpen())
{
selector.selectNow();
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while(iterator.hasNext())
{
SelectionKey key = iterator.next();
iterator.remove();
if(!key.isValid())
{
System.out.println("Invalid key removed!");
key.channel().close();
key.cancel();
continue;
}
if(key.isAcceptable())
{
server.accept().configureBlocking(false).register(selector, SelectionKey.OP_READ);
System.out.println("Accepting channel...");
continue;
}
if(key.isReadable())
{
SocketChannel channel = (SocketChannel) key.channel();
try
{
channel.read((ByteBuffer) buffer.clear());
if(buffer.flip().limit() == 0)
{
continue;
}
System.out.println(buffer.get() & 0xFF);
} catch(Exception e)
{
e.printStackTrace();
channel.close();
key.cancel();
}
}
}
}
}
And it's client:
public static void main(String[] args) throws Exception
{
try(Socket socket = new Socket("localhost", 724))
{
OutputStream output = socket.getOutputStream();
output.write(15);
output.flush();
}
}
Once the client is disconnected from the server, the load on the CPU increases up to 40%
I tried to find a solution, but the search has given nothing. I think this is due to the fact that the selector does not remove disconnect the client, but I do not know how to test - whether the client is disconnected or not. channel.isOpen() always returns true.