"Java NiO" nonblocking nio

Source: Internet
Author: User

1  PackageCom.slp.nio;2 3 Importorg.junit.Test;4 5 Importjava.io.IOException;6 Importjava.net.InetSocketAddress;7 ImportJava.nio.ByteBuffer;8 ImportJava.nio.channels.SelectionKey;9 ImportJava.nio.channels.Selector;Ten ImportJava.nio.channels.ServerSocketChannel; One ImportJava.nio.channels.SocketChannel; A ImportJava.time.LocalDateTime; - Importjava.util.Date; - ImportJava.util.Iterator; the ImportJava.util.Scanner; -  - /** - * Created by SANGLP on 2017/3/2. + * I. Three cores to complete network communication using NIO - * 1, Channel: Responsible for the connection + * |--java.nio.channels.channel Interface A * |--selectablechannel at * |--socketchannel - * |--serversocketchannel - * |--datagramchannel -  * - * |--pipe.sinkchannel - * |--pipe.sourcechannel in * 2, Buffer: Responsible for data access - * 3, selector: Selectablechannel multiplexer for monitoring the IO status of Selectablechannel to  * +  */ -  Public classTestnonblockingnio { the  * @Test $      Public voidClient ()throwsIOException {Panax Notoginseng         //Get channel -Socketchannel Socketchannel = Socketchannel.open (NewInetsocketaddress ("127.0.0.1", 9898)); the         //switch to non-blocking mode +Socketchannel.configureblocking (false); A         //Allocating buffers theBytebuffer buf = bytebuffer.allocate (1024); +         //send data to the server -  $Scanner Scanner =NewScanner (system.in); $          while(Scanner.hasnext ()) { -String str =Scanner.next (); -Buf.put ((NewDate (). toString () + "\ n" +str). GetBytes ()); the Buf.flip (); - Socketchannel.write (BUF);Wuyi buf.clear (); the         } -  Wu  -         //Close Channel About socketchannel.close (); $  -     } -  - @Test A      Public voidServer ()throwsIOException { +         //Get channel theServersocketchannel Serversocketchannel =Serversocketchannel.open (); -         //Toggle Non-blocking mode $Serversocketchannel.configureblocking (false); the         //Binding Connections theServersocketchannel.bind (NewInetsocketaddress (9898)); the         //Get a selector theSelector Selector =Selector.open (); -         //registers the channel with the selector and specifies that the listener receives the event in Serversocketchannel.register (selector, selectionkey.op_accept); the         //polling for an event that is already ready on the picker the          while(Selector.select () >0){ About             //gets all the registered selection keys in the current selector (Ready listener events) theIterator<selectionkey> it =Selector.selectedkeys (). iterator (); the              while(It.hasnext ()) { the                 //get ready-to-go events +Selectionkey SK =It.next (); -                 //determine what the event is, ready to go. the                 if(Sk.isacceptable ()) {Bayi                     //Get justifying connection theSocketchannel Socketchannel =serversocketchannel.accept (); the                     //Toggle Non-blocking mode -Socketchannel.configureblocking (false); -                     //register the channel on the selector the Socketchannel.register (selector,selectionkey.op_read); the}Else if(Sk.isreadable ()) { the                     //gets the channel for the read-ready state on the current selector theSocketchannel Socketchannel =(Socketchannel) Sk.channel (); -                     //reading Data theBytebuffer buffer = bytebuffer.allocate (1024); the                     intLen =0; the                      while(Len=socketchannel.read (buffer)) >0){94 Buffer.flip (); theSystem.out.println (NewString (Buffer.array (), 0, Len)); the buffer.clear (); the                     }98                 } About                 //deselect Key - It.remove ();101             }102         }103     }104}

"Java NiO" nonblocking nio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.