《java解读nio socket非阻塞模式(java解读nio socket非阻塞模式)》由会员分享,可在线阅读,更多相关《java解读nio socket非阻塞模式(java解读nio socket非阻塞模式)(24页珍藏版)》请在金锄头文库上搜索。
1、java解读nio socket非阻塞模式(java解读nio socket非阻塞模式)Wide sea diving, sky. I left my back, not bad accent metante. Wear butterfly butterfly deep see, water dragonflies fly slowly. This is the same root, fratricidal. Spring wind knows not bitter, do not send willow green. Java interpretation of NIO Socket non
2、 blocking mode (Reprinted) 2010, 05 October 20, 16:27 introduction Thursday:The non blocking I/O (NIO) provided by JDK effectively solves the thread overhead problem of multithreaded servers, but it seems a little more complicated in use. The use of multi thread in NIO, the main purpose is not to re
3、spond to each client request to allocate a separate service thread, but through multi thread waiting time to make full use of the processing capacity and use of a plurality of CPU, to improve the service ability of the.Its time to study NIO and write a blog to remember what you learned. Or start wit
4、h the simplest Hello World,Client multithreaded request server side, server receive the name of client, and return Hello + name character format to client. Of course, the actual application is not so simple. It might actually be accessing files or databases and getting information back to client. Wh
5、ats the mystery about non blocking NIO? Code:1) server side codeJava code/ * * author Jeff* /Public, class, HelloWorldServer Static int BLOCK = 1024;Static, String, name =;Protected Selector selector;Protected, ByteBuffer, clientBuffer = ByteBuffer.allocate (BLOCK);Protected CharsetDecoder decoder;S
6、tatic, CharsetEncoder, encoder = Charset.forName (GB2312),.NewEncoder ();Public HelloWorldServer (int port) throws IOException Selector = this.getSelector (port);Charset charset = Charset.forName (GB2312);Decoder = charset.newDecoder ();Get / / SelectorProtected, Selector, getSelector (int, port), t
7、hrows, IOException ServerSocketChannel server = ServerSocketChannel.open ();Selector sel = Selector.open ();Server.socket ().Bind (new, InetSocketAddress (port);Server.configureBlocking (false);Server.register (SEL, SelectionKey.OP_ACCEPT);Return sel;/ / listen portPublic, void, listen () tryFor (=)
8、 Selector.select ();Iterator ITER = selector.selectedKeys ().Iterator ();While (iter.hasNext () SelectionKey key = (SelectionKey) iter.next ();Iter.remove ();Process (key); catch (IOException, e) E.printStackTrace ();/ / eventProtected, void, process (SelectionKey, key), throws, IOException If (key.
9、isAcceptable) / / receiving requestserversocketchannel server = (serversocketchannel) key.channel ();socketchannel channel = server.accept ();/ / 设置非阻塞模式channel.configureblocking (false);channel.register (selector, selectionkey.op _ read). else if (key.isreadable () / / 读信息socketchannel channel = (s
10、ocketchannel) key.channel ();int count = channel.read (clientbuffer);if (count 0) clientbuffer.flip ();charbuffer charbuffer = decoder.decode (clientbuffer);name = charbuffer.tostring ();/ / system.out.println (name);selectionkey skey = channel.register (selector.selectionkey.op _ write).skey.attach
11、 (name); else channel.close ();clientbuffer.clear (); else if (key.iswritable () / / 写事件socketchannel channel = (socketchannel) key.channel ();string name = (string) key.attachment ();bytebuffer block = encoder.encode (charbuffer.wrap (hello! + name);channel.write (block)./ / channel.close ();public
12、 static void main (string args) int port = 8888;try helloworldserver server = new helloworldserver (port).system.out.println (listening on + port).server.listen (); catch (ioexception) e.printstacktrace ();/ * * author jeff* /public class helloworldserver static int block = 1024.static string name =
13、 ;protected selector selector;protected bytebuffer clientbuffer = bytebuffer.allocate (block).protected charsetdecoder decoder.static charsetencoder encoder = charset.forname (gb2312).newencoder ();public helloworldserver (int port) throws ioexception selector = this.getselector (port).charset = cha
14、rset charset.forname (gb2312);the decoder = charset.newdecoder ();/ / 获取selectorprotected selector getselector (int port) throws ioexception serversocketchannel server = serversocketchannel.open ();selector sel = selector.open();服务器。socket()。绑定(新inetsocketaddress(港);服务器configureblocking(假);服务器。登记(SEL,SelectionKey。op_accept);回归自我;/ /监听端口listen() public void尝试为了(;)select()选择器;迭代器iter =选择器。selectedkeys()。();而(ITER。hasnext())SelectionKey的关键=(SelectionKey)next() ITER;remove() ITER;过程(键);