Logs originally recorded in blogjavaHttp://www.blogjava.net/wfeng007/archive/2008/04/06/191112.html
It is mainly used to learn the basic mechanism of selector.
When I learned the NIO tutorial from IBM, I found thatCodeThere was a serious problem.
That is, when the client suddenly interrupts the TCP connection. The server enters a broken infinite loop.
Later it was found that the selector was able to select () The selectedkey of the socketchannel in the op_read status when the TCP connection was disconnected.
In this case, it is determined by the return value when the channel reads data to the buffer.
This reverse return value is actually the number of bytes read. When the value is 0, it indicates that no data can be read. When the value is-1, it indicates that the underlying TCP has been disconnected. (However, the IE connection is a little different. An exception is directly given during read. All of these conditions must be determined .)
Then I thought about how to cancel the association between selector and socketchannel? What is the basic internal operation of selector? The key. Cancel () method is written in javadoc. Socket close () seems to have the same function in windows.
Here we can use two graphs to represent the internal mechanism of several common selector actions.
First, the entire selector contains four key sets or arrays.