-1

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% enter image description here

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.

4

2 回答 2

2

From the Javadoc for Selector.selectNow()

This method performs a non-blocking selection operation. If no channels have become selectable since the previous selection operation then this method immediately returns zero.

Yes, that's going to burn CPU; it's non-blocking and you have a tight loop.

Edit to add: Generally the idea behind using a Selector is that you use the blocking select() or select(long timeout) versions which means as soon as a socket does something, it unblocks.

于 2013-08-01T00:44:07.293 回答
0

Found solution. I forgot close connection after EOS received.

if(channel.read((ByteBuffer) buffer.clear()) == -1)
{
    channel.close();
    continue;
}
于 2013-08-01T01:06:02.890 回答