In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Netty server how to solve the sticky package problem, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
1. What is sticky package / unpacking
The so-called TCP sticky packet is that the received data can not fully reflect a complete message data. Why is there a sticky package in TCP communication? The main reason is that TCP processes data in the way of stream, and the MTU on the network is often smaller than the message data processed in the application, so it will cause the received data can not meet the needs of the message, resulting in the existence of sticky packets. The only way to deal with sticky packets is to develop a data communication protocol in the application layer to regulate whether the existing received data meets the needs of message data.
2. Solution
2.1. The length of the message is fixed, the size of the message is fixed, and there is not enough space to complete. The sender and the receiver follow the same convention, so that even if the fixed-length message is obtained through the receiver's programming, it can be distinguished.
2.2. Add a special delimiter at the end of the packet, such as a carriage return newline character (such as FTP protocol) at the end of each message, or specify a special character as the message delimiter, and the receiver divides the message by the special separator.
2.3. The message is divided into a message header and a message body. The message header contains fields that represent the total length of the information (or the length of the message body).
3. Set a special delimiter on the netty server side
Note: the delimiter must be specified before the handler of data processing
@ Componentpublic class NettyServer {@ Value ("${netty.server.port}") private int port; / * launch netty server * @ throws InterruptedException * / public void start () {EventLoopGroup parentGroup = new NioEventLoopGroup (); EventLoopGroup childGroup = new NioEventLoopGroup (); try {ServerBootstrap serverBootstrap = new ServerBootstrap () ServerBootstrap.group (parentGroup, childGroup) .channel (NioServerSocketChannel.class) .option (ChannelOption.SO_BACKLOG, Integer.parseInt (CommonUtils.getConfig ("ChannelOption.SO_BACKLOG")) / / sets the thread queue to get the number of connections .option (ChannelOption.SO_KEEPALIVE) True) / / sets the active state of keeping connections. ChildHandler (new ChannelInitializer () {@ Override protected void initChannel (SocketChannel ch) {/ / Note that the handler handled by the message delimiter must precede the message processing logic ByteBuf buf = Unpooled.copiedBuffer ("\ t") Charset.defaultCharset () / / Custom unpacking character / / (1024) the maximum length of the decoded frame, false (whether to remove the specified character), buf (unpacking character) DelimiterBasedFrameDecoder delimiterBasedFrameDecoder = new DelimiterBasedFrameDecoder (1400, true, buf); ch.pipeline () .addLast (delimiterBasedFrameDecoder) Ch.pipeline () .addLast (new NettyServerHandler2 ());}}); ChannelFuture channelFuture = serverBootstrap.bind (port) .sync (); channelFuture.channel () .closeFuture () .sync ();} catch (InterruptedException e) {e.printStackTrace () } finally {try {parentGroup.shutdownGracefully (). Sync (); childGroup.shutdownGracefully (). Sync ();} catch (InterruptedException e) {e.printStackTrace ();} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.