1. Is the Key. Attachment empty? SC. register (selector, selectionkey. op_write | selectionkey. op_read, BB); when using this method to register an event, you need to add the third parameter: attachment object; or use key. interestops (selectionkey. op_write | selectionkey. op_read) method;
2. How to capture client disconnection events? When the read is disconnected, the readable key is triggered. Therefore, in isreadable, check whether the returned value of read is-1.
3. If you do not register a write event, the write operation is undoubtedly the same as the synchronization method. If you have registered a write event, you must cancel the event when writing the event.
4. Key. Cancel cancels both read and write events.
Package my; import Java. io. ioexception; import Java. io. unsupportedencodingexception; import java.net. inetsocketaddress; import java.net. serversocket; import Java. NIO. bytebuffer; import Java. NIO. channels. selectionkey; import Java. NIO. channels. selector; import Java. NIO. channels. serversocketchannel; import Java. NIO. channels. socketchannel; import Java. util. iterator; public class NiO {public static final int size = 1024; void start (INT port) {bytebuffer buffer = bytebuffer. allocate (size); try {serversocketchannel S = serversocketchannel. open (); serversocket socket = S. socket (); selector = selector. open (); inetsocketaddress ADDR = new inetsocketaddress (port); socket. BIND (ADDR); S. configureblocking (false); S. register (selector, selectionkey. op_accept); While (true) {int selcount = selector. select (); iterator <Selectionkey> keys = selector. selectedkeys (). iterator (); While (keys. hasnext () {system. out. println ("select count:" + selcount); selectionkey key = (selectionkey) keys. next (); keys. remove (); If (key. isacceptable () {system. out. println ("isacc... "); serversocketchannel SSC = (serversocketchannel) Key. channel (); socketchannel SC = SSC. accept (); SC. configureblocking (false); SC. register (selector, selectionkey. Op_read);} else if (key. isreadable () {system. out. println ("readable .. "); socketchannel SC = (socketchannel) Key. channel (); try {int COUNT = SC. read (buffer); system. out. println ("read byte:" + count); // log (buffer); If (COUNT = size) {// If count is equal to the buffer size, it indicates that the message has not been read. // The message structure of the complex point needs to be designed.} elseif (COUNT =-1) {// The read event is triggered when the client is disconnected, and you can read-1, then close the SC channel. close (); key. cancel (); Continue ;}} catch (exception e) {// todo: handle ex Ceptionsc. Close (); key. Cancel (); system. Out. println ("close! "); Continue;} buffer. flip (); bytebuffer BB = buffer. duplicate (); key. attach (bb); buffer. clear (); // SC. register (selector, // selectionkey. op_write | selectionkey. op_read, BB); // equivalent to key. interestops (selectionkey. op_write | selectionkey. op_read); // generally, the write event is not registered unless the written content is huge. After the write event is registered, cancel it.} else if (key. iswritable () {system. out. println ("writable .. "); socketchannel SC = (socketchannel) Key. channel (); bytebuffer ATT = (bytebuffer) Key. attachment (); If (ATT. hasremaining () {int COUNT = SC. write (ATT); system. out. println ("write:" + Count + "byte, hasremain:" + Att. hasremaining ();} else {// After writing, cancel the writeable event and only listen to the readable event key. interestops (selectionkey. op_read) ;}}}} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public static void log (bytebuffer BUF) {try {system. out. println (new string (BUF. array (), 0, Buf. limit (), "utf8");} catch (unsupportedencodingexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public static void main (string [] ARGs) {New NiO (). start (802 );}}
Java NiO notes (1)