Netty instances-Simple service-side-client implementation, gazing at specific

Source: Internet
Author: User

Book Recommendation: example code: http://download.csdn.net/detail/jiangtao_st/7677503

w=unionnojs&f=http%3a%2f%2fai.taobao.com%2fauction%2fedetail.htm%3fe%3dnwfw% 252fe17lvwjmraedzvrln4d8gsqrsnltcbl1om% 252br1klltg5xficodxrtutgh9smdpiwxrc30rhswkknj6ztyddmkr6k2rzzwtzgpwlbfzanzymzz1ozjeizwr1bmnhu%26unid%3d28416740 %26ptype%3d100010%26from%3dbasic&k=5ccfdb950740ca16&c=un&b=alimm_0&p=mm_28416740_6610563_ 22760419 ">

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamlhbmd0yw9fc3q=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

  1. Netty Server-side implementation
    /** * * <p> * Netty Server Simple * </p> * * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public class Ne Ttyserver {private final int port = 8989; @Testpublic void Nettyserver () {Eventloopgroup bossgroup = new Nioeventloopgroup () ; Eventloopgroup Workergroup = new Nioeventloopgroup (); try {serverbootstrap serverbootstrap = new Serverbootstrap (); Serverbootstrap.group (Bossgroup,workergroup). Channel (nioserversocketchannel.class). Option (Channeloption.so_ BACKLOG, 1024x768). Childhandler (New Childchannelhandler ());//Bind port, synchronous wait channelfuture futrue = serverbootstrap.bind (port) . sync ();//wait for the service listener port to close Futrue.channel (). Closefuture (). sync (); catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally{//exit, release thread and other related resources bossgroup.shutdowngracefully (); workergroup.shutdowngracefully ();}} Private class Childchannelhandler extends channelinitializer<socketchannel>{@Overrideprotected void Initchannel (Socketchannel ch) throws Exception {Ch.pipeline (). AddLast (New SimpLeserverhandler ());}}} 

  2. Netty Client Implementation

    /** *  * <p> * nettyclient  Implementation * </p> *  * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public CLA SS Nettyclient {public void connect (int port,string host) {Eventloopgroup group = new Nioeventloopgroup (); try {Bootstrap bo Otstrap = new Bootstrap (); Bootstrap.group (group). Channel (niosocketchannel.class). Option (Channeloption.tcp_nodelay , true). Handler (new channelinitializer<socketchannel> () {@Overrideprotected void Initchannel (Socketchannel ch) Throws Exception {Ch.pipeline (). AddLast (New Simpleclienthandler ());}); /initiates an asynchronous link operation channelfuture channelfuture = Bootstrap.connect (host, port). sync (); Channelfuture.channel (). Closefuture (). Sync ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally{//off, releasing thread resources group.shutdowngracefully ();}} @Testpublic void Nettyclient () {new Nettyclient (). Connect (8989, "localhost");}}


  3. Serverhander processing Program

    /** *  * <p> * Server Receive Message Processing handler * </p> *  * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public cl Simpleserverhandler extends Channelinboundhandleradapter {@Overridepublic void Channelread (Channelhandlercontext CTX, Object msg) throws Exception {bytebuf buf = (bytebuf) msg;byte [] req = new Byte[buf.readablebytes ()];buf.readbytes (re Q); String message = new String (req, "UTF-8"); SYSTEM.OUT.PRINTLN ("netty-server:receive message," + message);}}


  4. Clienthander Processing program

    /** *  * <p> * Client Handler * </p> *  * @author Check Inn HK * @ creation Time: July 7, 2014 * @version: V1.0 */public class S Impleclienthandler extends Channelinboundhandleradapter {private bytebuf clientmessage;public Simpleclienthandler () { byte [] req = "Call-user-service". GetBytes (); clientmessage = Unpooled.buffer (req.length); Clientmessage.writebytes ( REQ);} @Overridepublic void Channelactive (Channelhandlercontext ctx) throws Exception {Ctx.writeandflush (clientmessage);}  @Overridepublic void Channelread (Channelhandlercontext ctx, Object msg) throws Exception {bytebuf buf = (bytebuf) msg;byte [] req = new Byte[buf.readablebytes ()];buf.readbytes (req); String message = new String (req, "UTF-8"); SYSTEM.OUT.PRINTLN ("netty-client:receive message," + message);} @Overridepublic void Exceptioncaught (Channelhandlercontext ctx, Throwable cause) throws Exception {Ctx.close ();}}


Netty instances-Simple service-side-client implementation, gazing at specific

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.