Netty solve the problem of TCP sticky packet and unpacking (iii)

Source: Internet
Author: User
Tags ming

Decode today using the fixed length of the Netty

The principle of fixed-length decoding is to automatically decode messages according to the length of the specified message.

In the Netty implementation, only need to use the Fiexedlengthframedecoder decoder can ...

The following is the service-side code

1  PackageCom.ming.netty.nio.stickpack;2 3 4 5 Importjava.net.InetSocketAddress;6 7 ImportIo.netty.bootstrap.ServerBootstrap;8 Importio.netty.channel.ChannelFuture;9 ImportIo.netty.channel.ChannelInitializer;Ten Importio.netty.channel.ChannelOption; One ImportIo.netty.channel.EventLoopGroup; A ImportIo.netty.channel.nio.NioEventLoopGroup; - ImportIo.netty.channel.socket.SocketChannel; - ImportIo.netty.channel.socket.nio.NioServerSocketChannel; the ImportIo.netty.handler.codec.FixedLengthFrameDecoder; - ImportIo.netty.handler.codec.string.StringDecoder; - ImportIo.netty.handler.logging.LogLevel; - ImportIo.netty.handler.logging.LoggingHandler; +  -  Public classEchoserver { +  A      Public voidBind (String addr,intPortthrowsexception{ atEventloopgroup bossgroup=NewNioeventloopgroup (); -Eventloopgroup workgroup=NewNioeventloopgroup (); -         Try { -Serverbootstrap server=Newserverbootstrap (); - Server.group (Bossgroup,workgroup) -. Channel (Nioserversocketchannel.class) in. option (Channeloption.so_backlog, 100) -. Handler (NewLogginghandler (loglevel.info)) to. Childhandler (NewChannelinitializer<socketchannel>() { +  - @Override the                     protected voidInitchannel (Socketchannel SC)throwsException { *Sc.pipeline (). AddLast (NewFixedlengthframedecoder (50));//Fixedlengthframedecoder This class is a fixed-length decoder, meaning that the message is specified in length to decode $Sc.pipeline (). AddLast (NewStringdecoder ());Panax NotoginsengSc.pipeline (). AddLast (NewEchoserverhandler ()); -                          the                     } +                        A                 }); theChannelfuture F=server.bind (Newinetsocketaddress (addr, port)). sync (); +SYSTEM.OUT.PRINTLN ("Start server:" +F.channel (). localaddress ()); -             //wait, the server-side listening port is off $ F.channel (). Closefuture (). sync (); $}Catch(Exception e) { - e.printstacktrace (); -}finally{ the bossgroup.shutdowngracefully (); - workgroup.shutdowngracefully ();Wuyi         } the     } -      Wu      -      Public Static voidMain (string[] args)throwsexception{ About         NewEchoserver (). Bind ("192.168.1.108", 8500); $     } -      -}
1  PackageCom.ming.netty.nio.stickpack;2 3 ImportIo.netty.buffer.ByteBuf;4 Importio.netty.buffer.Unpooled;5 ImportIo.netty.channel.ChannelHandlerAdapter;6 ImportIo.netty.channel.ChannelHandlerContext;7 8  Public classEchoserverhandlerextendschannelhandleradapter{9 Ten     intCount=0; One      A @Override -      Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException { -          theString body=(String) msg; -SYSTEM.OUT.PRINTLN ("Server Received" + (++count) + "Secondary client message, message is:" +body); -body+= "$_"; -Bytebuf rep=Unpooled.copiedbuffer (Body.getbytes ()); + Ctx.writeandflush (rep); -     } +  A @Override at      Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException { - cause.printstacktrace (); - ctx.close (); -     } -  -      in}

Note: Fixedlengthframedecoder This class decoding, your client will each time according to the specified length of data received, haha ... In the example is the 50-length word Fuha ...

Learn every day, progress every day ...

I think it's a good idea.

Netty solve the problem of TCP sticky packet and unpacking (iii)

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.