Netty client-to-server communication (ii)
I. INTRODUCTION
In the previous blog post, I wrote about the Netty entry-level Hello World program. The book on the back, this blog is about the client and the service side of the communication, feel there is nothing to say, directly on the code bar.
Two. Client-to-server communication
2.1 Server-side startup program
Public classMyServer { Public Static voidMain (string[] args)throwsinterruptedexception {eventloopgroup bossgroup=NewNioeventloopgroup (); Eventloopgroup Workergroup=NewNioeventloopgroup (); Try{serverbootstrap Serverbootstrap=NewServerbootstrap (); Serverbootstrap.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Childhandler (NewMyinitializer ()); Channelfuture channelfuture= Serverbootstrap.bind (8899). sync (); Channelfuture.channel (). Closefuture (). sync (); }finally{bossgroup.shutdowngracefully (); Workergroup.shutdowngracefully (); } }}
2.2 Service-side channel initialization program
Public classMyinitializerextendsChannelinitializer<socketchannel>{@Overrideprotected voidInitchannel (Socketchannel ch)throwsException {Channelpipeline pipeline=Ch.pipeline (); /** * Lengthfieldbasedframedecoder: The frame decoder based on the length attribute. * The data format sent by the client is: * before DECODE (bytes) after DECODE (bytes) * +--------+------------ ----+ +--------+----------------+ * | Length | Actual Content |----->| Length | Actual Content | * | 0x000c | "HELLO, World" | | 0x000c | "HELLO, World" | * +--------+----------------+ +--------+----------------+ * 5 parameters are: 1. (maxframelength) The maximum length of data per frame. * 2. (Lengthfieldoffset) The offset of the length property in the frame. * 3. (lengthfieldlength) Length property needs to be consistent with the length of the client Lengthfieldprepender setting, * value can only be 1, 2, 3, 4, 8 * 4. (lengthadjustment) Length adjustment value, which is used to correct the length of the information when the length of the message is included. * 5. (Initialbytestostrip) The length (usually length) to ignore when acquiring real content. * * Reference: http://blog.csdn.net/educast/article/details/47706599 */Pipeline.addlast ("Lengthfieldbasedframedecoder", NewLengthfieldbasedframedecoder (Integer.max_value, 0, 2, 0, 2)); /*** The length of the Lengthfieldprepender:length property in the frame. Only for 1,2,3,4,8. * This value is consistent with the value of the lengthfieldlength * specified in Lengthfieldbasedframedecoder when decoding with the corresponding client (or server). */Pipeline.addlast ("Lengthfieldprepender",NewLengthfieldprepender (3)); //a decoder for stringdecoder strings, primarily for processing encoding formatsPipeline.addlast ("Stringdecoder",NewStringdecoder (charsetutil.utf_8)); //an encoder for the Stringdecoder string, used primarily to specify the encoding format of a stringPipeline.addlast ("Stringencoder",NewStringencoder (charsetutil.utf_8)); Pipeline.addlast (NewMyHandler ());//Custom Handler }}
2.3 Custom Handler
Public classMyHandlerextendsSimplechannelinboundhandler<string>{@Overrideprotected voidChannelRead0 (Channelhandlercontext ctx, String msg)throwsException {System.out.println (Ctx.channel (). remoteaddress ()+ ":" +msg); Ctx.channel (). Writeandflush ("From server: Grass Mud Horse"); } @Override Public voidhandleradded (Channelhandlercontext ctx)throwsException {System.out.println (System.currenttimemillis ()+ "********"); System.out.println ("Server Handler added**********"); } @Override Public voidChannelregistered (Channelhandlercontext ctx)throwsException {System.out.println (System.currenttimemillis ()+ "********"); System.out.println ("Server Channel register****"); } @Override Public voidChannelactive (Channelhandlercontext ctx)throwsException {System.out.println (System.currenttimemillis ()+ "********"); System.out.println ("Server Channel actieve****"); } @Override Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException {cause.printstacktrace (); Ctx.close (); }}
2.4 Client Startup program
Public classmyclient { Public Static voidMain (string[] args)throwsinterruptedexception {eventloopgroup eventloopgroup=NewNioeventloopgroup (); Try{Bootstrap Bootstrap=NewBootstrap (); Bootstrap.group (Eventloopgroup). Channel (Niosocketchannel.class). Handler (NewClientinitializer ()); Channelfuture channelfuture= Bootstrap.connect ("127.0.0.1", 8899). sync (); Channelfuture.channel (). Closefuture (). sync (); }finally{eventloopgroup.shutdowngracefully (); } }}
2.5 Client channel Initialization
Public classClientinitializerextendsChannelinitializer<socketchannel>{@Overrideprotected voidInitchannel (Socketchannel ch)throwsException {Channelpipeline pipeline=Ch.pipeline (); Pipeline.addlast ("Lengthfieldbasedframedecoder", NewLengthfieldbasedframedecoder (Integer.max_value, 0, 2, 0, 2)); Pipeline.addlast ("Lengthfieldprepender",NewLengthfieldprepender (3)); Pipeline.addlast ("Stringdecoder",NewStringdecoder (charsetutil.utf_8)); Pipeline.addlast ("Stringencoder",NewStringencoder (charsetutil.utf_8)); Pipeline.addlast (NewMyclienthandler ()); }}
2.5 Client Custom Handler
Public classMyclienthandlerextendsSimplechannelinboundhandler<string>{@Overrideprotected voidChannelRead0 (Channelhandlercontext ctx, String msg)throwsException {System.out.println (Ctx.channel (). remoteaddress ()); System.out.println (msg); Ctx.channel (). Writeandflush ("To Server: Grass Mud Horse"); } @Override Public voidChannelactive (Channelhandlercontext ctx)throwsException {System.out.println (System.currenttimemillis ()+ "..........."); Ctx.channel (). Writeandflush ("Greetings from the client!" "); System.out.println ("Client channel Active ..."); } @Override Public voidhandleradded (Channelhandlercontext ctx)throwsException {System.out.println (System.currenttimemillis ()+ "..........."); System.out.println ("Client Hanlder added ..."); } @Override Public voidChannelregistered (Channelhandlercontext ctx)throwsException {System.out.println (System.currenttimemillis ()+ "..........."); System.out.println ("Client Channel Register ..."); } @Override Public voidChannelinactive (Channelhandlercontext ctx)throwsException {System.out.println ("Client Channel inactive ..."); } @Override Public voidChannelunregistered (Channelhandlercontext ctx)throwsException {System.out.println ("Client Channel unregister ..."); } @Override Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException {cause.printstacktrace (); Ctx.close (); }}
Three. Run the test
Run the service-side startup code, and then run the client startup code, and you can see the thousands "Grass mud horse" in the collapse.
Netty client-to-server communication (ii)