~/ learn/ comp-348/ cards/ Channels: SocketChannel and ServerSocketChannel
1 of 5

Open a non-blocking server channel and accept without waiting

Open a non-blocking server channel and accept without waiting

Answer

ServerSocketChannel server = ServerSocketChannel.open(); server.bind(new InetSocketAddress(7)); server.configureBlocking(false); SocketChannel client = server.accept();

open() only creates the object — bind() is what starts it listening, and accept() before bind() throws NotYetBoundException. After configureBlocking(false), accept() returns null rather than waiting, so the very next line must test for null.

Harold 4e ch11 §Channels

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/5880897b-76ae-49d6-8bc3-a11453b1b655/flashcard utf-8 LF