Before for the sake of the mode and convenience has not dealt with the problem of sticky bag, today devoted to the time to engage in netty of sticky packet processing, to know that in high concurrency, do not handle sticky packets is not possible, the chaos of data flow will cause business crashes or something I will not say. So this question has always been a knot in my heart.
The use of Netty really happy, used to write to the service side of the problem of self-treatment of sticky pack of all kinds of pain but that is also the basic skills of things.
There's a couple of netty inside the bag. I'm using Lengthfileldbasedframedecoder, which is used to parse packets with a length attribute, as long as we add the total length of the packet to the transport protocol.
Arg0.pipeline (). AddFirst ("Decoder", New Lengthfieldbasedframedecoder (1024,0,4,0,4));
Arg0.pipeline (). AddLast (New Testinlistener ());
Lengthfieldbasedframedecoder
The meaning of several parameters
1, the maximum length
2-3, describe the packet length, because I used 4 bytes to describe the length of the entire package here tells the package length described in the first 4 bytes of the unpacking
4-5, if the length value of the whole package length contains 4 bytes of Baotou, then tell the unpacking from 0 to 4th bytes without interception
The final unpacking package is called handler. The data stream you get here is what's been intercepted, and it doesn't contain the first 4 bytes.
@Override Public voidChannelread (Channelhandlercontext ctx, Object msg) throws Exception {String ResultStr=""; //first turn into Netty bufBytebuf result =(BYTEBUF) msg; //All Data byte[] Alldatabyte =New byte[Result.readablebytes ()]; //turn into a byte arrayresult.readbytes (Alldatabyte); ResultStr=NewString (Alldatabyte,"UTF-8"); }
All right, now, let's talk about my problems.
First the server is used Netty but the client is C # before the unpacking of the time to read the first 4 bytes There is no problem with the unpacking the problem, because,
The whole package sent by C # over there is a small-end mode that causes Netty to take the first 4 bytes out of the package and solve the length error ~netty there is no handling of this,
So C # Send the previous 4 bytes to the big end before sending it OK
What is big-endian small end, in fact, this name is a bit of a pit dad will cause confusion, because the end for the Chinese people have the beginning of meaning after this call it small tail, big tail
Small tail is the low address of the data stored in the high and low address the status of the data what the hell?
Look at the picture
Look at the small end mode is high address small data storage
Small end is a small tail is big tail.
Service-side Netty client non-netty handling sticky and unpacking issues