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)