Skip to content

Commit 14f0c18

Browse files
netty拆包器
1 parent 4e90a8d commit 14f0c18

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

src/main/java/com/netty/echo/EchoClient.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.netty.echo;
22

3+
import com.netty.decoder.ClientNoDecoderHandler;
34
import io.netty.bootstrap.Bootstrap;
45
import io.netty.channel.*;
56
import io.netty.channel.nio.NioEventLoopGroup;
67
import io.netty.channel.socket.SocketChannel;
78
import io.netty.channel.socket.nio.NioSocketChannel;
9+
import io.netty.handler.codec.FixedLengthFrameDecoder;
810

911
/**
10-
* 客户端。发送数据给服务端,并接收服务端的响应。
12+
* 客户端。发送消息给服务端,并接收服务端的响应。
1113
*
1214
*/
1315
public 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-

src/main/java/com/netty/echo/EchoServer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.netty.channel.nio.NioEventLoopGroup;
66
import io.netty.channel.socket.SocketChannel;
77
import io.netty.channel.socket.nio.NioServerSocketChannel;
8+
import io.netty.handler.codec.FixedLengthFrameDecoder;
89
import io.netty.handler.logging.LogLevel;
910
import 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-

0 commit comments

Comments
 (0)