Java--nio-tcp Socket

Source: Internet
Author: User

1, First we use socketchannel, to implement the socket client

 packagecom.seeyon.nio.socket;Importjava.io.IOException;Importjava.net.InetSocketAddress;Importjava.nio.ByteBuffer;Importjava.nio.channels.SocketChannel;/*** Created by Yangyu on 2017/2/22.*/ public classClient { public Static voidmain (string[] Args) {Try(socketchannel Socketchannel = Socketchannel.open (NewInetsocketaddress ("localhost", 8088)) {socketchannel.configureblocking (false); Bytebuffer Bytebuffer= Bytebuffer.allocate (512); Socketchannel.write (bytebuffer.wrap ("this is client send message". GetBytes ()));  while(true) {bytebuffer.clear (); intReadbytes =Socketchannel.read (bytebuffer); if(readbytes > 0) {bytebuffer.flip (); System.out.println (NewString (bytebuffer.array (), 0, readbytes));                    Socketchannel.close ();  break; }            }        } Catch(ioexception E) {e.printstacktrace (); }    }}

2, use Serversocketchannel to implement the service side, and use selector

 packagecom.seeyon.nio.socket;Importjava.io.IOException;Importjava.net.InetSocketAddress;Importjava.nio.ByteBuffer;Importjava.nio.channels.SelectionKey;Importjava.nio.channels.Selector;Importjava.nio.channels.ServerSocketChannel;Importjava.nio.channels.SocketChannel;Importjava.util.Iterator;Importjava.util.Set;/*** Created by Yangyu on 2017/2/22.*/ public classServer { public Static voidMain (string[] Args)throwsIOException {serversocketchannel Serverchannel=Serversocketchannel.open (); Serverchannel.socket (). bind (NewInetsocketaddress (8088)); Serverchannel.configureblocking (false); Selector Selector=Selector.open ();        Serverchannel.register (selector, selectionkey.op_accept);  while(true) {selector.select (); Set<SelectionKey> Readykeys =Selector.selectedkeys (); Iterator<SelectionKey> iterator =Readykeys.iterator ();  while(iterator.hasnext ()) {selectionkey key=Iterator.next ();                Iterator.remove (); if(key.isacceptable ()) {System.out.println ("accept"); Bytebuffer Bytebuffer= Bytebuffer.allocate (512); Serversocketchannel Serversocketchannel=(serversocketchannel) Key.channel (); Socketchannel Socketchannel=serversocketchannel.accept (); Socketchannel.configureblocking (false); intReadkeys =Socketchannel.read (bytebuffer); if(readkeys > 0) {bytebuffer.flip (); System.out.println (NewString (bytebuffer.array (), 0, readkeys)); } socketchannel.write (bytebuffer.wrap ("received". GetBytes ())); } Else if(key.isreadable ()) {System.out.println ("read"); } Else if(key.iswritable ()) {System.out.println ("write"); }            }        }    }}

Java--nio-tcp Socket

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.