0. There are multiple channels. Key. Channel () returns the host of the event. If the key is an op_accept event, serversocketchannel is returned.
1. The selectionkey. Cancel: Cancel () method permanently cancels selectionkey. opxxxx and puts it in the canceled set of the selector. During the next call to the select () method, these keys are removed from all the key sets of the selector, and their associated channels are not listened to (unless it is re-registered ).
2. Nio HTTP server example:
Import Java. Io .*; Import Java. NiO .*; Import Java. NiO. channels .*; Import Java. util. iterator; Import Java.net .*; Public Class Nonblockingsinglefilehttpserver {Private Bytebuffer contentbuffer; Private Int Port = 80; Public Nonblockingsinglefilehttpserver (bytebuffer data, string encoding, string mimetype, Int Port) Throws Unsupportedencodingexception { This . Port = port; string header =" HTTP/1.0 200 OK \ r \ n "+" Server: onefile 2.0 \ r \ n "+" Content-Length: "+ Data. Limit () +"\ R \ n "+" Content-Type: "+ Mimetype +" \ R \ n "; Byte [] Headerdata = header. getbytes (" ASCII "); Bytebuffer buffer = bytebuffer. allocate (data. limit () + headerdata. length); buffer. put (headerdata); buffer. put (data); buffer. flip (); This . Contentbuffer = buffer ;} Public Void Run () Throws Ioexception {serversocketchannel serverchannel = serversocketchannel. open (); serversocket = serverchannel. socket (); selector = selector. open (); inetsocketaddress localport = New Inetsocketaddress (port); serversocket. BIND (localport); serverchannel. configureblocking ( False ); // The server channel registers only accept events Serverchannel. Register (selector, selectionkey. op_accept ); While ( True ) {Selector. Select (); iterator keys = selector. selectedkeys (). iterator (); While (Keys. hasnext () {selectionkey key = (selectionkey) keys. Next (); // Be sure to delete the processed event. Otherwise, the next select operation will still be submitted to you. Keys. Remove (); Try { If (Key. isacceptable () {serversocketchannel Server = (serversocketchannel) Key. Channel (); socketchannel channel = server. Accept (); Channel. configureblocking ( False ); // Register a readable event for the client Channel Selectionkey newkey = channel. Register (selector, selectionkey. op_read );} Else If (Key. iswritable () {socketchannel channel = (socketchannel) Key. Channel (); bytebuffer buffer = (bytebuffer) Key. Attachment (); Synchronized (Buffer ){ If (Buffer. hasremaining ()){ // Write-only Channel. Write (buffer );} Else { // We're done // Close the client connection after writing Channel. Close ();}}} Else If (Key. isreadable ()){ // Don't bother trying to parse the HTTP header. // Just read something. Socketchannel channel = (socketchannel) Key. Channel (); bytebuffer buffer = bytebuffer. Allocate (4096); Channel. Read (buffer ); // Buffer. Flip (); // System. Out. println (new string (buffer. Array ())); // Switch channel to write-only mode // Change to only register writable events Key. interestops (selectionkey. op_write); key. Attach (contentbuffer. Duplicate ());}} Catch (Ioexception ex) {ex. printstacktrace ();// Cancel the event corresponding to the key Key. Cancel (); Try {Key. Channel (). Close ();} Catch (Ioexception cex) {cex. printstacktrace ();}}}}} Public Static Void Main (string [] ARGs) {ARGs = New String [] {" I: \ description _readme.html "," 801 "," Gb2312 "};If (ARGs. Length = 0) {system. Out. println (" Usage: Java nonblockingsinglefilehttpserver file port Encoding "); Return ;} Try {String contenttype =" Text/plain "; If (ARGs [0]. endswith (" . Html ") | ARGs [0]. endswith (" . Htm ") {Contenttype =" Text/html ";} Fileinputstream fin = New Fileinputstream (ARGs [0]); filechannel in = fin. getchannel (); bytebuffer input = in. Map (filechannel. mapmode. read_only, 0, In. Size ()); // Set the port to listen on Int Port; Try {Port = integer. parseint (ARGs [1]); If (Port <1 | Port> 65535) Port = 80 ;} Catch (Exception ex) {Port = 80;} string encoding =" ASCII "; If (ARGs. length> 2) encoding = ARGs [2]; nonblockingsinglefilehttpserver Server =New Nonblockingsinglefilehttpserver (input, encoding, contenttype, Port); server. Run ();} Catch (Exception ex) {ex. printstacktrace (); system. Err. println (Ex );}}}