NOTE
Selector-Based NIO Server
Designing a Java non-blocking server with Selector, per-connection state, partial I/O, and backpressure.
This is a historical learning note and may contain outdated or incomplete understanding.
A selector-based server typically:
- opens a non-blocking
ServerSocketChannel; - registers it for accept readiness;
- waits in
Selector.select(); - accepts connections and registers socket channels;
- handles read/write readiness using connection-specific state.
Never assume one readable event equals one message. TCP is a byte stream, so decode frames across arbitrary read boundaries.
Write readiness also needs care: keep unsent bytes in a bounded output buffer and register for write interest only while data remains. Otherwise a slow client can turn unbounded application output into a memory problem.