File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .netty .decoder ;
2+
3+ import io .netty .buffer .ByteBuf ;
4+ import io .netty .channel .ChannelHandlerContext ;
5+ import io .netty .channel .ChannelInboundHandlerAdapter ;
6+
7+ import java .nio .charset .StandardCharsets ;
8+
9+ public class ClientNoDecoderHandler extends ChannelInboundHandlerAdapter {
10+ @ Override
11+ public void channelActive (ChannelHandlerContext ctx ) {
12+ for (int i = 0 ; i < 1000 ; i ++) {
13+ ByteBuf buffer = getByteBuf (ctx );
14+ ctx .channel ().writeAndFlush (buffer );
15+ }
16+ }
17+
18+ private ByteBuf getByteBuf (ChannelHandlerContext ctx ) {
19+ byte [] bytes = "粘包拆包测试." .getBytes (StandardCharsets .UTF_8 );
20+ ByteBuf buffer = ctx .alloc ().buffer ();
21+ buffer .writeBytes (bytes );
22+ return buffer ;
23+ }
24+ }
Original file line number Diff line number Diff line change 11package com .netty .echo ;
22
3+ import com .netty .decoder .ClientNoDecoderHandler ;
34import io .netty .bootstrap .Bootstrap ;
45import io .netty .channel .*;
56import io .netty .channel .nio .NioEventLoopGroup ;
67import io .netty .channel .socket .SocketChannel ;
78import io .netty .channel .socket .nio .NioSocketChannel ;
9+ import io .netty .handler .codec .FixedLengthFrameDecoder ;
810
911/**
10- * 客户端。发送数据给服务端 ,并接收服务端的响应。
12+ * 客户端。发送消息给服务端 ,并接收服务端的响应。
1113 *
1214 */
1315public final class EchoClient {
@@ -27,9 +29,11 @@ public static void main(String[] args) throws Exception {
2729 @ Override
2830 public void initChannel (SocketChannel socketChannel ) throws Exception {
2931 ChannelPipeline channelPipeline = socketChannel .pipeline ();
30- //channelPipeline.addLast(new LoggingHandler(LogLevel.INFO));
3132 //ChannelHandler,用于处理 channel,实现对接收的数据的处理,实现业务逻辑。
32- channelPipeline .addLast (new EchoClientHandler ());
33+ //固定长度的拆包器 FixedLengthFrameDecoder
34+ channelPipeline .addLast (new FixedLengthFrameDecoder (19 ));
35+ channelPipeline .addLast (new ClientNoDecoderHandler ());
36+ // channelPipeline.addLast(new EchoClientHandler());
3337 }
3438 });
3539
@@ -42,4 +46,3 @@ public void initChannel(SocketChannel socketChannel) throws Exception {
4246 }
4347 }
4448}
45-
Original file line number Diff line number Diff line change 55import io .netty .channel .nio .NioEventLoopGroup ;
66import io .netty .channel .socket .SocketChannel ;
77import io .netty .channel .socket .nio .NioServerSocketChannel ;
8+ import io .netty .handler .codec .FixedLengthFrameDecoder ;
89import io .netty .handler .logging .LogLevel ;
910import io .netty .handler .logging .LoggingHandler ;
1011
@@ -30,15 +31,16 @@ public static void main(String[] args) throws Exception {
3031 //指定 channel
3132 .channel (NioServerSocketChannel .class )
3233 .option (ChannelOption .SO_BACKLOG , 100 )
33- //指定 ChannelHandler,用于处理 channel
34+ //指定 ChannelHandler,用于处理 Channel
3435 .handler (new LoggingHandler (LogLevel .INFO ))
3536 .childHandler (new ChannelInitializer <SocketChannel >() {
3637 @ Override
3738 public void initChannel (SocketChannel ch ) throws Exception {
3839 //ChannelPipeline,基于责任链模式,可以添加多个 ChannelHandler
3940 ChannelPipeline channelPipeline = ch .pipeline ();
40- //channelPipeline.addLast(new LoggingHandler(LogLevel.INFO));
4141 //ChannelHandler,用于处理 channel,实现对接收的数据的处理,实现业务逻辑。
42+ //固定长度的拆包器 FixedLengthFrameDecoder
43+ channelPipeline .addLast (new FixedLengthFrameDecoder (19 ));
4244 channelPipeline .addLast (new EchoServerHandler ());
4345 }
4446 });
@@ -55,5 +57,3 @@ public void initChannel(SocketChannel ch) throws Exception {
5557 }
5658 }
5759}
58-
59-
You can’t perform that action at this time.
0 commit comments