In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the introduction of TCP sticky package and the definition of message in Netty". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "the introduction of TCP sticky package and the definition of message in Netty".
1. The problem of sticking and unpacking of TCP 1.1, the graphical solution of the problem of gluing and unpacking.
Assuming that the client sends two packets D1 and D2 respectively, because the server is uncertain about the number of bytes read at one time, there are five possible situations:
The server reads two independent packets, D1 and D2, without sticking and unpacking.
The server receives two packets at a time. D1 and D2 are glued together, which is called TCP sticky packet.
The server reads two packets twice, the first time it reads part of the D2 packet, and the second time it reads the rest of the D2 packet and the complete D1 packet, which is called TCP unpacking.
The server reads two packets twice, the first time it reads the complete D2 packet and part of the D1 packet, and the second time it reads the rest of the D1 packet.
If the TCP receiving sliding window of the server is very small, and the packet D1 and D2 are relatively large, the server can receive the D1 and D2 packets multiple times. During this period, multiple unpacking will occur.
1.2. Sticky package problem solving strategy
The message is fixed in length. For example, fix the size of each message to 100 bytes, and if not, fill in the blanks.
The message header is divided into the message header and the message body, and the message header contains the fields of the total message length (or message body length).
A carriage return newline character is added at the end of the package for segmentation.
2. Message definition in Netty protocol stack
The message in the Netty protocol stack is divided into two parts:
Message header
Message body
3. Conversion between byte array (byte []) and string (hexadecimal / Base64) 3.1, byte [] hexadecimal string / * hexadecimal string and byte array conversion tool class * / public class HexUtils {private static final char [] HEXES = {'0,1,' 2, 3, 3, 4, 5, 6' 7 days, 8 months, 9 weeks, 9 months, 9 months, 9 months / * byte array converted to hexadecimal lowercase string * / public static String bytes2Hex (byte [] bytes) {if (bytes = = null | | bytes.length = = 0) {return null;} StringBuilder hex = new StringBuilder (); for (byte b: bytes) {hex.append (HEXES [(b > > 4) & 0x0F]) Hex.append (HEXES [b & 0x0F]);} return hex.toString ();} / * hexadecimal string converted to the corresponding byte array * / public static byte [] hex2Bytes (String hex) {if (hex = = null | | hex.length () = = 0) {return null;} char [] hexChars = hex.toCharArray () Byte [] bytes = new byte [hexChars.length / 2]; / / if there are not even characters in the hex, ignore the last for (int I = 0; I < bytes.length; icharacters +) {bytes [I] = (byte) Integer.parseInt ("" + hexChars [I * 2] + hexChars [I * 2 + 1], 16);} return bytes }}-author: xietansheng original: https://blog.csdn.net/xietansheng/article/details/884216553.2 byte [] Base64 string import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream / * Base64 conversion tool * / public class Base64Utils {/ * byte array to Base64 string * / public static String encode (byte [] data) {return new BASE64Encoder () .encode (data) } / * Base64 string converted to byte array * / public static byte [] decode (String base64) {try {return new BASE64Decoder () .decodeBuffer (base64);} catch (IOException e) {e.printStackTrace ();} return new byte [0] } / * encode the contents of a file as a Base64 string, which can only encode small files (such as text, pictures, etc.) * / public static String encodeFile (File file) throws Exception {InputStream in = null; ByteArrayOutputStream bytesOut = null; try {in = new FileInputStream (file); bytesOut = new ByteArrayOutputStream ((int) file.length ()) Byte [] buf = new byte [1024]; int len =-1; while ((len = in.read (buf))! =-1) {bytesOut.write (buf, 0, len);} bytesOut.flush (); return encode (bytesOut.toByteArray ());} finally {close (in) Close (bytesOut);} / * convert the Base64 string to a byte array and save it to the specified file * / public static void decodeFile (String base64, File file) throws Exception {OutputStream fileOut = null; try {fileOut = new FileOutputStream (file); fileOut.write (decode (base64)); fileOut.flush () } finally {close (fileOut);}} private static void close (Closeable c) {if (c! = null) {try {c.close () } catch (IOException e) {/ / nothing} Thank you for your reading. The above is the content of "introduction of TCP sticky package and definition of message in Netty". After the study of this article, I believe you have a deeper understanding of the introduction of TCP sticky package and the definition of message in Netty, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.