Netty Socket Programming (IV)

Source: Internet
Author: User

Netty provided by handler: Codec, message header, message encoding

1. After the connection is established, neither the client nor the server will initiate the message and implement the handler channelactive to trigger the message sending.

overloaded methods for 2.SimpleChannelInboundHandler

(1) call when handleradded connection is established

(2) called when the handlerremoved connection is broken

(3) Handleractive Connection is active call

(4) The handlerinactive connection is in an inactive state call

Socket Example:

Server Startup class:

1  Public classMyServer {2 3      Public Static voidMain (string[] args)throwsinterruptedexception {4 5Eventloopgroup Bossgroup =NewNioeventloopgroup ();6Eventloopgroup Workergroup =NewNioeventloopgroup ();7 8         Try{9Serverbootstrap Serverbootstrap =Newserverbootstrap ();Ten Serverbootstrap.group (bossgroup,workergroup). OneChannel (Nioserversocketchannel.class) A. Childhandler (NewMyserverinitializer ()); -Channelfuture channelfuture = Serverbootstrap.bind (8899). sync (); - Channelfuture.channel (). Closefuture (). sync (); the  -}finally { -  - bossgroup.shutdowngracefully (); + workergroup.shutdowngracefully (); -  +         } A  at     } -  -}

Server Initialization class:

1  Public classMyserverinitializerextendsChannelinitializer<socketchannel>{2 3 4 @Override5     protected voidInitchannel (Socketchannel ch)throwsException {6 7Channelpipeline Channelpipeline =ch.pipeline ();8 9         //Decoding DeviceTenChannelpipeline.addlast (NewLengthfieldbasedframedecoder (integer.max_value,0,4,0,4)); One         //Message plus Header AChannelpipeline.addlast (NewLengthfieldprepender (4)); -Channelpipeline.addlast (NewStringdecoder (Charsetutil.utf_8)); -Channelpipeline.addlast (NewStringencoder (Charsetutil.utf_8)); theChannelpipeline.addlast (NewMyserverhandler ()); -  -     } -}

Server Business Processing handler:

1  Public classMyserverhandlerextendsSimplechannelinboundhandler<string> {2 @Override3     protected voidChannelRead0 (Channelhandlercontext ctx, String msg)throwsException {4 5System.out.println (Ctx.channel (). Remoteaddress () + "msg:" +msg);6 7Ctx.channel (). Writeandflush ("from server" +Uuid.randomuuid ());8 9     }Ten  One @Override A      Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException { - cause.printstacktrace (); - ctx.close (); the     } -}

Client class :

Client Startup class:

1  Public classmyclient {2 3      Public Static voidMain (string[] args)throwsinterruptedexception {4 5Eventloopgroup Eventloopgroup =NewNioeventloopgroup ();6 7         Try{8Bootstrap Bootstrap =NewBootstrap ();9Bootstrap.group (Eventloopgroup). Channel (Niosocketchannel.class)Ten. Handler (NewMyclientinitializer ()); One  AChannelfuture channelfuture = Bootstrap.connect ("localhost", 8899). sync (); - Channelfuture.channel (). Closefuture (). sync (); -}finally { the eventloopgroup.shutdowngracefully (); -         } -  -  +     } -  +}

Client Initialization class:

1  Public classMyclientinitializerextendsChannelinitializer<socketchannel>{2 @Override3     protected voidInitchannel (Socketchannel ch)throwsException {4Channelpipeline Channelpipeline =ch.pipeline ();5         //Decoding Device6Channelpipeline.addlast (NewLengthfieldbasedframedecoder (integer.max_value,0,4,0,4));7         //Message plus Header8Channelpipeline.addlast (NewLengthfieldprepender (4));9Channelpipeline.addlast (NewStringdecoder (Charsetutil.utf_8));TenChannelpipeline.addlast (NewStringencoder (Charsetutil.utf_8)); OneChannelpipeline.addlast (NewMyclienthandler ());//your own processor. A     } -}

Client Business Processing Handler:

1  Public classMyclienthandlerextendsSimplechannelinboundhandler<string>{2 @Override3     protected voidChannelRead0 (Channelhandlercontext ctx, String msg)throwsException {4 System.out.println (Ctx.channel (). remoteaddress ());5SYSTEM.OUT.PRINTLN ("Client Output" +msg);6Ctx.writeandflush ("from client" +Localdatetime.now ());7 8     }9 Ten @Override One      Public voidChannelactive (Channelhandlercontext ctx)throwsException { ACtx.writeandflush ("Greetings from the client!! "); -     } -  the @Override -      Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException { - cause.printstacktrace (); - ctx.close (); +     } -}

Netty Socket Programming (IV)

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.