Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept of TCP sticky package unpacking and an example of TCP sticky package unpacking solved by Netty

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "TCP sticky package unpacking concept and Netty solution TCP sticky package unpacking example", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn the "TCP sticky package unpacking concept and Netty solution TCP sticky package unpacking example" bar!

1. TCP adhesive packet unpacking operating system

As we all know, the core of the operating system is the kernel, which is independent of ordinary applications and has access to protected memory space and all permissions to the underlying hardware devices.

In order to protect the user process from directly accessing the operating kernel and ensure the security of the kernel, the operating system is divided into two parts, one is the kernel space, the other is the user space.

Ipaw O model

For the sake of the security of the operating system, the process cannot access the Imax O device directly, and the kernel must request the kernel to complete the Imax O operation through the system call, and the kernel maintains a buffer (buffer) for each Imax O device.

The whole request process is as follows:

1. The user process initiates a request. After the kernel receives the request, it acquires the data from the Icano device to the buffer.

2. Copy the data in buffer to the address space of the user process

3. The user process obtains the data and then responds to the client

Sticking and unpacking

Similarly, when the operating system sends data through the TCP protocol, it will first store the data in the buffer, assuming that the buffer size is 1024 bytes.

Sticky bag

If the packet sent is small, much smaller than the size of the buffer, TCP will merge multiple packets into a single packet to send, which leads to sticky packets.

Unpack

If the packet sent is large, much larger than the size of the buffer, TCP will split the packet into multiple packets to send, which leads to packet unpacking.

The server reads two separate packets, D1 and D2, without sticking and unpacking.

The server receives two packets at a time. D1 and D2 are glued together, and the sticky packet occurs.

The server read two packets twice, once read the complete D1 packet and D2 part of the packet D2room1, and the second time read the remaining D2 packet D2room2

The server read two packets in two times, once read the partial D1 D1room1, and the second time read the remaining D1 packets D1room2 and complete D2 packets.

2. Solve the problem of TCP sticking and unpacking

For the problem of TCP sticky package unpacking, there are four solutions

(1) fixed message length, for example, the size of each packet is 128 bytes. If it is not enough, fill in the blanks.

(2) the client uses a fixed delimiter at the end of each package, such as\ r\ n.If a package is split, it waits for the next package to be sent to find the\ r\ n.Then the split header is merged with the rest of the previous package, and a complete package is obtained.

(3) the message is divided into the header and the message body. The current length of the entire message is saved in the header. Only after the message of sufficient length is read can a complete message be read.

(4) sticking and unpacking through a custom protocol

3. Netty sticky package unpacking example

The client sends three messages to the server, and the server also replies to the client after receiving the message.

Client public class HelloWorldClient {private int port; private String address; public HelloWorldClient (int port, String address) {this.port = port; this.address = address;} public void start () {EventLoopGroup group = new NioEventLoopGroup (); Bootstrap bootstrap = new Bootstrap () Bootstrap.group (group) .channel (NioSocketChannel.class) .handler (new ChannelInitializer () {@ Override protected void initChannel (SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline (); pipeline.addLast ("decoder", new StringDecoder ()) / / string decoding and encoding pipeline.addLast ("encoder", new StringEncoder ()) Pipeline.addLast ("handler", new ChannelInboundHandlerAdapter () {@ Override public void channelRead (ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println ("client receives message: [" > server public class HelloWorldServer {private int port) Public HelloWorldServer (int port) {this.port = port;} public void start () {EventLoopGroup bossGroup = new NioEventLoopGroup (); / / create a parent-child thread group EventLoopGroup workGroup = new NioEventLoopGroup (); ServerBootstrap server = new ServerBootstrap () Server.group (bossGroup, workGroup) .channel (NioServerSocketChannel.class) / / specifies the channel to process the client. ChildHandler (new ChannelInitializer () {@ Override protected void initChannel (SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline () Pipeline.addLast ("decoder", new StringDecoder ()); / / string decoding and encoding pipeline.addLast ("encoder", new StringEncoder ()) Pipeline.addLast ("handler", new ChannelInboundHandlerAdapter () {@ Override public void channelRead (ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println ("the server receives a message: [" + msg + "]") Ctx.writeAndFlush (Unpooled.copiedBuffer ("I am the server" .getBytes ()); ctx.writeAndFlush (Unpooled.copiedBuffer ("I am the server" .getBytes ()); ctx.writeAndFlush (Unpooled.copiedBuffer ("I am the server" .getBytes () }}); / / Custom handler}}); / / Channel initialization try {ChannelFuture future = server.bind (port). Sync (); future.channel (). CloseFuture (). Sync ();} catch (InterruptedException e) {e.printStackTrace () } finally {bossGroup.shutdownGracefully (); workGroup.shutdownGracefully ();}} public static void main (String [] args) {HelloWorldServer server = new HelloWorldServer (8888); server.start ();}}

Running result:

Server side

Client

You can see that the three messages received by the client are all stuck together.

4. Netty solves sticking and unpacking 4.1.The fixed length solution is adopted.

The client adds a fixed-length FixedLengthFrameDecoder message decoder, and the server does not have to deal with it.

Running result

You can see that the message received by the client is indeed divided by the number of bytes, but because the length of a message is more than 10 bytes, garbled code will occur when Chinese characters are encountered, which is also the deficiency of the fixed-length separator.

4.2. Use delimiters

The client modifies the initChannel method and defines $as the delimiter

The server adds a custom delimiter $at the end of the message

Then the message received by the client is a separate message, and there is no sticky packet.

Thank you for your reading, the above is the "TCP sticky package unpacking concept and Netty solution TCP sticky package unpacking example" content, after the study of this article, I believe you on the concept of TCP sticky package unpacking and Netty solution TCP sticky package unpacking example of this problem has a more profound experience, the specific use of the need for you to practice verification. 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report