Java NIO Primer "original"

Source: Internet
Author: User

Server
 PackageCom.server;Importjava.net.InetSocketAddress;ImportJava.nio.ByteBuffer;ImportJava.nio.channels.ServerSocketChannel;ImportJava.nio.channels.SocketChannel; Public classServer { Public Static voidMain (string[] args)throwsException {//new TCP service sideServersocketchannel Serversocketchannel =Serversocketchannel.open (); //Binding TCP PortsServersocketchannel.socket (). Bind (NewInetsocketaddress (9999)); //configured as "non-blocking"Serversocketchannel.configureblocking (false);  while(true) {            //allow TCP links to be receivedSocketchannel Socketchannel =serversocketchannel.accept (); //When there is a TCP connection, the fetch is not empty            if(Socketchannel! =NULL) {                //writing English Data "life is not a step, fighting is endless"String NewData = System.currenttimemillis () + ": Cease to struggle and your cease to live."; //Open CacheBytebuffer buf = bytebuffer.allocate (1024); //Reset , ready to write data to the cachebuf.clear (); //actually writing data to the cacheBuf.put (Newdata.getbytes ()); //preparing to read data from the cacheBuf.flip (); //If you read the data with the remaining                 while(Buf.hasremaining ()) {//actually reads the data from the cache and writes it to the channelSocketchannel.write (BUF); }            }        }    }}

Client
 Packagecom.client;Importjava.net.InetSocketAddress;ImportJava.nio.ByteBuffer;ImportJava.nio.channels.SocketChannel; Public classClient { Public Static voidMain (string[] args)throwsException {//new TCP service sideSocketchannel Socketchannel =Socketchannel.open (); //link to local 8888 portSocketchannel.connect (NewInetsocketaddress ("localhost", 8888)); //Open CacheBytebuffer buf = bytebuffer.allocate (1024); //Reset , ready to write data to the cachebuf.clear (); //actually reads data from the channel to the cache        intBytesread =Socketchannel.read (BUF); //preparing to read data from the cacheBuf.flip (); //If you read the data with the remaining         while(Buf.hasremaining ()) {//Take one byte            byteB = (byte) (Buf.get ()); //turn into a characterSystem.out.print ((Char) b); }        //Close ChannelSocketchannel.close (); }}

Java NIO Primer "original"

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.