The treatment of the netty of the sticky bag

Source: Internet
Author: User
Tags jboss

1, Netty in the byte array when the transfer, there will be sticky packets and sub-package situation. When the data is OK, if the amount of data is large. and continuously sent to the server, this time there will be sticky packets and sub-package situation.

2, simple: Channelbuffer in the receiving packet, will be processed at that time, but when the amount of data a large, this time the separation of data is not very obvious. There will be more or less data at this time.

3, the way of processing, is generally a codec. Define your own data: data length + data. This simple way to achieve. The decryption operation is performed on the server.

4, the specific implementation process

A, the client code

Package Com.troy.application.buffer;import Java.io.ioexception;import java.net.inetsocketaddress;import Java.nio.bytebuffer;import Java.nio.channels.SocketChannel; Public classClient { Public Static voidMain (string[] args) {Try{Socketchannel Socketchannel=Socketchannel.open (); Socketchannel.connect (NewInetsocketaddress ("localhost",9000)); //data that needs to be sentString message ="Hello"; //The 4 here represents the byte of the length of the messageBytebuffer Bytebuffer = Bytebuffer.allocate (4+message.length ()); //Setting up dataBytebuffer.putint (Message.length ());            Bytebuffer.put (Message.getbytes ()); //Write DataSocketchannel.write (Bytebuffer.wrap (Bytebuffer.array ())); } Catch(IOException e) {e.printstacktrace (); }    }}

b, the service side

Package Com.troy.application.buffer;import Org.jboss.netty.bootstrap.serverbootstrap;import Org.jboss.netty.channel.channelpipeline;import Org.jboss.netty.channel.channelpipelinefactory;import Org.jboss.netty.channel.channels;import Org.jboss.netty.channel.socket.nio.nioserversocketchannelfactory;import Java.net.inetsocketaddress;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Public classServer { Public Static voidMain (string[] args) {//declaring a service classServerbootstrap Serverbootstrap =NewServerbootstrap (); //Set Thread poolExecutorservice boss =Executors.newcachedthreadpool (); Executorservice Work=Executors.newcachedthreadpool (); //set up a factoryServerbootstrap.setfactory (Newnioserversocketchannelfactory (boss,work)); //set up pipeline flowServerbootstrap.setpipelinefactory (Newchannelpipelinefactory () {@Override PublicChannelpipeline Getpipeline () throws Exception {Channelpipeline Channelpipeline=Channels.pipeline (); //add a processing methodChannelpipeline.addlast ("Decode",NewPackagedecoder ()); Channelpipeline.addlast ("Hello",NewHellohandler ()); returnChannelpipeline;        }        }); //Setting up PortsServerbootstrap.bind (NewInetsocketaddress (9000)); }}

C, decoding

Package Com.troy.application.buffer;import Org.jboss.netty.buffer.channelbuffer;import Org.jboss.netty.channel.channel;import Org.jboss.netty.channel.channelhandlercontext;import Org.jboss.netty.handler.codec.oneone.OneToOneDecoder; Public classPackagedecoder extends Onetoonedecoder {@OverrideprotectedObject Decode (Channelhandlercontext Channelhandlercontext, Channel Channel, Object O) throws Exception {Chann Elbuffer Channelbuffer=(Channelbuffer) o; if(Channelbuffer.readablebytes () >4) {            //Tag Read locationChannelbuffer.markreaderindex (); //Read Data length            intn =Channelbuffer.readint (); if(Channelbuffer.readablebytes () <N) {//If the data length is less than the set data, the cache stateChannelbuffer.resetreaderindex (); //Cache Current data and wait for data access                return NULL; }            byte[] bytes =New byte[n];            Channelbuffer.readbytes (bytes); return NewString (bytes); }        //Cache Current data and wait for data access        return NULL; }}

d, data reception processing

Package Com.troy.application.buffer;import Org.jboss.netty.channel.channelhandlercontext;import Org.jboss.netty.channel.messageevent;import Org.jboss.netty.channel.SimpleChannelHandler;  Public class Hellohandler extends simplechannelhandler{    @Override    publicvoid  Messagereceived (Channelhandlercontext ctx, messageevent e) throws Exception {        System.  out . println (E.getmessage ());}    }

The treatment of the netty of the sticky bag

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.