Netty 4.0 Object Transfer

Source: Internet
Author: User
Tags throwable

1 for the service side,

Private void bindport (Int port) {eventloopgroup workgroup = new  Nioeventloopgroup (); Eventloopgroup bossgroup = new nioeventloopgroup ();try{serverbootstrap b =  New serverbootstrap (); B.group (Bossgroup, workgroup); B.channel (Nioserversocketchannel.class)  . Option (channeloption.so_backlog, 128)  .handler (New logginghandler (loglevel.info))  . Childhandler (new channelinitializer<socketchannel> ()  {@Overrideprotected  void  Initchannel (Socketchannel ch)  throws exception {ch.pipeline (). AddLast (new  Objectdecoder (1024*1024, classresolvers.weakcachingconcurrentresolver (This.getclass (). GetClassLoader ()))) ; Ch.pipeline (). AddLast (New objectencoder ()); Ch.pipeline (). AddLast (New serverreqhandler ());}); Channelfuture channelfuture = b.bind (port). sync (); Channelfuture.channel (). Closefuture (). sync (); catch (exception e) {}finally{Workgroup.shutdowngracefully (); bossgroup.shutdowngracefully ();}} class serverreqhandler extends channelinboundhandleradapter{@Overridepublic  void  Exceptioncaught (Channelhandlercontext ctx, throwable cause) throws exception { Cause.printstacktrace (); Ctx.close ();} @Overridepublic  void channelactive (CHANNELHANDLERCONTEXT&NBSP;CTX)  throws exception { System.out.println ("channel active");} @Overridepublic  void channelread (channelhandlercontext ctx, object msg) throws  exception {servermsg servermsg =  (servermsg)  msg; System.out.println (servermsg);}}

Before adding your own handler (Serverreqhandler), add Objectdecoder and Objectencoder,

Their handler can be directly forced after receiving classes, note here that the transmitted class (SERVERMSG) must implement the Serializable Interface (serialization)


2. For client

Private void connect (Int port) {eventloopgroup workgrop = new  Nioeventloopgroup (); Try{bootstrap bootstrap = new bootstrap (); Bootstrap.group (WorkGrop); Bootstrap.channel (Niosocketchannel.class)  .option (channeloption.tcp_nodelay, true)  .handler (new  ChannelInitializer<Channel> ()  {@Overrideprotected  void initchannel (channel ch)  throws exception {ch.pipeline (). AddLast (New objectdecoder (1024*1024,   Classresolvers.weakcachingconcurrentresolver (This.getclass (). getClassLoader ()))); Ch.pipeline (). AddLast (New  objectencoder ()); Ch.pipeline (). AddLast (New clientreqhandler ());}); Channelfuture channelfuture = bootstrap.connect ("localhost",  port). sync (); Channelfuture.channel (). Closefuture (). sync (); catch (exception e) {e.printstacktrace ();} Finally{workgrop.shutdowngracefully ();}  }class clientreqhandler extends channelinboundhandleradapter{@Overridepublic  void channelactive (channelhandlercontext ctx)  throws  Exception {for (int i = 0; i < 10; i++) {ServerMsg msg =  parseservermsg (i); Ctx.write (msg);} Ctx.flush ();} Private servermsg parseservermsg (int id) {servermsg msg = new servermsg (); Msg.setid (ID); Msg.setname (id +  ""); return msg;} @Overridepublic  void exceptioncaught (Channelhandlercontext ctx, throwable cause) Throws exception {cause.printstacktrace (); Ctx.close ();}} The handler to be added is the same as the server, in its own handler (Clientreqhandler), send msg directly with ctx  servermsgpublic class servermsg  implements serializable {private static final long serialversionuid =  1l;private int id;private string name;public int getid ()  {return  id;} Public void setid (Int id) &NBSP;{THIS.ID&NBsp;= id;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} @Overridepublic  string tostring ()  {return  "id: "  + id +  "  name:  " + name;}}


This article is from the "cleaner" blog, make sure to keep this source http://chpn208.blog.51cto.com/3115852/1630524

Netty 4.0 Object Transfer

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.