Package COM. mzj. NIO. java; import Java. io. ioexception; import java.net. inetsocketaddress; import Java. NIO. bytebuffer; import Java. NIO. channels. selectionkey; import Java. NIO. channels. selector; import Java. NIO. channels. socketchannel; import Java. util. iterator; import Java. util. set;/*** copyright (C), HTF <br> * nio communication client ** @ author muzhongjiang * @ Date February */public class nioclient {Private Static final Int Port = 8888; Private Static int block = 4096; // buffer size Private Static bytebuffer sendbuffer = bytebuffer. allocate (Block); // accept the data buffer Private Static bytebuffer receivebuffer = bytebuffer. allocate (Block); // send the data buffer private final static inetsocketaddress server_address = new inetsocketaddress ("localhost", Port); // The server address Private Static int flag = 0; // identify the number public static void main (string [] ARGs) THR Ows ioexception {socketchannel = socketchannel. open (); // open the socket channel socketchannel. configureblocking (false); // sets the non-blocking mode selector = selector. open (); // open the selector socketchannel. register (selector, selectionkey. op_connect); // register the socket action socketchannel to connect to the server. connect (server_address); // connection // allocate buffer size memory set <selectionkey> selectionkeys; iterator <selectionkey> iterator; selectionkey selectio Nkey; socketchannel client; string receivetext; string sendtext; int COUNT = 0; while (true) {// select a group of keys, the corresponding channel is ready for I/O operations. // This method selects in blocking mode. Selector. Select (); // returns the selected key set of this selector. Selectionkeys = selector. selectedkeys (); // system. out. println (selectionkeys. size (); iterator = selectionkeys. iterator (); While (iterator. hasnext () {selectionkey = iterator. next (); If (selectionkey. isconnectable () {system. out. println ("client connect"); client = (socketchannel) selectionkey. channel (); // determine whether the connection operation is ongoing on this channel. // Complete the socket channel connection process. If (client. isconnectionpending () {client. finishconnect (); system. Out. println ("Connection completed! "); Sendbuffer. clear (); sendbuffer. put ("Hello, server ". getbytes (); sendbuffer. flip (); client. write (sendbuffer);} client. register (selector, selectionkey. op_read);} else if (selectionkey. isreadable () {client = (socketchannel) selectionkey. channel (); // clear the buffer for the next read of receivebuffer. clear (); // read the data sent by the server to the buffer. Count = client. read (receivebuffer); If (count> 0) {receivetext = new string (receivebuffer. array (), 0, count); system. out. println ("the client accepts server-side data --:" + deleetext); client. register (selector, selectionkey. op_write) ;}} else if (selectionkey. iswritable () {sendbuffer. clear (); client = (socketchannel) selectionkey. channel (); sendtext = "Message from client --" + (flag ++); sendbuffer. put (sendtext. getbytes (); // reset the buffer flag, because the put data flag is changed. to read data from it and send it to the server, reset sendbuffer. flip (); client. write (sendbuffer); system. out. println ("the client sends data to the server --:" + sendtext); client. register (selector, selectionkey. op_read) ;}} selectionkeys. clear ();}}}
Package COM. mzj. NIO. java; import Java. io. ioexception; 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; import Java. util. set;/*** copyright (C), HTF <br> * nio Communication Server ** @ author muzho Ngjiang * @ date August 1, 2014 */public class nioserver {Private Static int Port = 8888; Private Static int flag = 0; // identifies the number Private Static int block = 4096; // buffer size Private Static bytebuffer sendbuffer = bytebuffer. allocate (Block); // accept the data buffer Private Static bytebuffer receivebuffer = bytebuffer. allocate (Block); // send data buffer Private Static selector; public static void start () throws ioexception {// 1. open the server socket channel serversocketchannel = serversocketchannel. open (); // 2. server configuration is non-blocking serversocketchannel. configureblocking (false); // retrieves the server socket serversocket = serversocketchannel associated with this channel. socket (); // bind serversocket to the service. BIND (New inetsocketaddress (port); // you can use the open () method to find selector = selector. open (); // register with selector and wait for the connection to serversocketchannel. register (selec Tor, selectionkey. op_accept); system. err. println ("Server start ----" + port); startlisten () ;}// listen to Private Static void startlisten () throws ioexception {While (true) {// select a set of keys, and the selector has been enabled for the corresponding channel. select (); // return the selected key set of this selector. Set <selectionkey> selectionkeys = selector. selectedkeys (); iterator <selectionkey> iterator = selectionkeys. iterator (); While (iterator. hasnext () {selectionkey = iterator. next (); iterator. remove (); handlekey (selectionkey) ;}}// process the request private static void handlekey (selectionkey) throws ioexception {// accept the request serversocketchannel Server = NULL; socketchannel client = NULL; String shortetext; string sendtext; int COUNT = 0; // test whether the channel of this key is ready to accept the new socket connection. If (selectionkey. isacceptable () {// return the channel for this key creation. Server = (serversocketchannel) selectionkey. Channel (); // receives the connection to this channel socket. // The socket channel returned by this method (if any) will be in blocking mode. Client = server. accept (); // The configuration is non-blocking client. configureblocking (false); // register with selector and wait for the client to be connected. register (selector, selectionkey. op_read);} else if (selectionkey. isreadable () {// return the channel for which this key is created. Client = (socketchannel) selectionkey. channel (); // clear the buffer for the next read of receivebuffer. clear (); // read the data sent by the server to the buffer. Count = client. read (receivebuffer); If (count> 0) {receivetext = new string (receivebuffer. array (), 0, count); system. out. println ("the Server accepts client data --:" + deleetext); client. register (selector, selectionkey. op_write) ;}} else if (selectionkey. iswritable () {// clear the buffer for the next write to sendbuffer. clear (); // Return the channel for this key. Client = (socketchannel) selectionkey. channel (); sendtext = "Message from server --" + flag ++; // input data to the buffer zone sendbuffer. put (sendtext. getbytes (); // reset the buffer flag, because the put data flag is changed. to read data from it and send it to the server, reset sendbuffer. flip (); // output to the channel client. write (sendbuffer); system. out. println ("the server sends data to the client --:" + sendtext); client. register (selector, selectionkey. op_read) ;}} public static void main (string [] ARGs) throws ioexception {start ();}}