netty粘包半包自定义填充长度解码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package com.io.netty.netty.netty12; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; import java.util.Arrays; import java.util.Random; @Slf4j public class HelloWorldClient { //填充,每次填充10个字节,不足10字节填满到10字节 public static byte [] fill0Bytes( char c, int len) { byte [] result = new byte [ 10 ]; Arrays.fill(result, ( byte ) '_' ); byte charByte = ( byte ) c; for ( int i = 0 ; i < len; i++) { result[i] = charByte; } return result; } static void start() { NioEventLoopGroup worker = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.channel(NioSocketChannel. class ); bootstrap.group(worker); bootstrap.handler( new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ch.pipeline().addLast( new LoggingHandler(LogLevel.DEBUG)); ch.pipeline().addLast( new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf buf=ctx.alloc().buffer(); char c = '0' ; Random random = new Random(); for ( int i = 0 ; i < 10 ; i++) { byte [] bytes = fill0Bytes(c, random.nextInt( 10 ) + 1 ); c++; buf.writeBytes(bytes); } ctx.writeAndFlush(buf); } }); } }); ChannelFuture localhost = bootstrap.connect( "localhost" , 9091 ).sync(); localhost.channel().closeFuture().sync(); } catch (InterruptedException e) { log.debug( "server error" , e); } finally { worker.shutdownGracefully(); } } public static void main(String[] args) { start(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package com.io.netty.netty.netty12; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; import java.util.Arrays; import java.util.Random; @Slf4j public class HelloWorldClient { //填充,每次填充10个字节,不足10字节填满到10字节 public static byte [] fill0Bytes( char c, int len) { byte [] result = new byte [ 10 ]; Arrays.fill(result, ( byte ) '_' ); byte charByte = ( byte ) c; for ( int i = 0 ; i < len; i++) { result[i] = charByte; } return result; } static void start() { NioEventLoopGroup worker = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.channel(NioSocketChannel. class ); bootstrap.group(worker); bootstrap.handler( new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ch.pipeline().addLast( new LoggingHandler(LogLevel.DEBUG)); ch.pipeline().addLast( new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf buf=ctx.alloc().buffer(); char c = '0' ; Random random = new Random(); for ( int i = 0 ; i < 10 ; i++) { byte [] bytes = fill0Bytes(c, random.nextInt( 10 ) + 1 ); c++; buf.writeBytes(bytes); } ctx.writeAndFlush(buf); } }); } }); ChannelFuture localhost = bootstrap.connect( "localhost" , 9091 ).sync(); localhost.channel().closeFuture().sync(); } catch (InterruptedException e) { log.debug( "server error" , e); } finally { worker.shutdownGracefully(); } } public static void main(String[] args) { start(); } } |
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/18707857
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?