NOTE

3.6 Server Implemented with NIO

NIO vs BIO and common questions about channel reads, selector.select, and SelectionKey.OP_WRITE.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

NIO vs BIO

BIO NIO
ServerSocket ServerSocketChannel
Socket SocketChannel
Selector
SelectionKey

Common Questions

An Exception and Infinite Loop Occur When the Client Closes

The reason is that the return value of channel.read(buffer) was not checked. A return value of -1 means that the client has closed the connection and reading cannot continue.

Why Is NIO Called Non-Blocking If selector.select Blocks?

selector.select//blocking
selector.select(1000)//non-blocking
selector.wakeup//wake up selector

What Does SelectionKey.OP_WRITE Mean?

It means that the underlying buffer has space available for writing.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub