NIO - Networking

suggest change

Versions

Selectable channels and selectors have been in the library since Java SE 1.4.

Remarks

SelectionKey defines the different selectable operations and information between its Selector and Channel. In particular, the attachment can be used to store connection-related information.

Handling OP_READ is pretty straightforward. However, care should be taken when dealing with OP_WRITE: most of the time, data can be written to sockets, so the event will keep firing. Make sure to register OP_WRITE only before you want to write data (see that answer).

Also, the OP_CONNECT interest should be removed once the channel has connected (because, well, it is connected. See this and that answer on Stack Overflow). Hence the OP_CONNECT removal after finishConnect() succeeded.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents