Skip to content

Commit e41a879

Browse files
author
yimkong
committed
添加半包解码handler解决粘包问题
1 parent fc1ce39 commit e41a879

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/main/java/TimeClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.netty.channel.nio.NioEventLoopGroup;
66
import io.netty.channel.socket.SocketChannel;
77
import io.netty.channel.socket.nio.NioSocketChannel;
8+
import io.netty.handler.codec.LineBasedFrameDecoder;
9+
import io.netty.handler.codec.string.StringDecoder;
810

911
/**
1012
* author yg
@@ -22,6 +24,8 @@ private void connect(int port, String host) throws Exception {
2224
.handler(new ChannelInitializer<SocketChannel>() {
2325
@Override
2426
protected void initChannel(SocketChannel ch) throws Exception {
27+
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
28+
ch.pipeline().addLast(new StringDecoder());
2529
ch.pipeline().addLast(new TimeClientHandler());
2630
}
2731
});
@@ -33,8 +37,12 @@ protected void initChannel(SocketChannel ch) throws Exception {
3337

3438
}
3539

36-
public static void main(String[] args) throws Exception {
40+
public static void main(String[] args) {
3741
int port = 8080;
38-
new TimeClient().connect(port,"localhost");
42+
try {
43+
new TimeClient().connect(port,"localhost");
44+
} catch (Exception e) {
45+
e.printStackTrace();
46+
}
3947
}
4048
}

src/main/java/TimeClientHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import io.netty.channel.ChannelHandlerContext;
44
import io.netty.channel.ChannelInboundHandlerAdapter;
55

6-
import java.nio.charset.StandardCharsets;
7-
86
/**
97
* author yg
108
* description
@@ -30,10 +28,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
3028

3129
@Override
3230
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
33-
ByteBuf byteBuf = (ByteBuf) msg;
34-
byte[] req = new byte[byteBuf.readableBytes()];
35-
byteBuf.readBytes(req);
36-
String body = new String(req, StandardCharsets.UTF_8);
31+
String body = (String) msg;
3732
System.out.println("body:" + body + ",counter:" + ++counter);
3833
}
3934

src/main/java/TimeServer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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.LineBasedFrameDecoder;
9+
import io.netty.handler.codec.string.StringDecoder;
810

911
/**
1012
* author yg
@@ -34,6 +36,8 @@ private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
3436

3537
@Override
3638
protected void initChannel(SocketChannel ch) throws Exception {
39+
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
40+
ch.pipeline().addLast(new StringDecoder());
3741
ch.pipeline().addLast(new TimeServerHandler());
3842
}
3943
}

src/main/java/TimeServerHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.netty.channel.ChannelHandlerContext;
44
import io.netty.channel.ChannelInboundHandlerAdapter;
55

6-
import java.nio.charset.StandardCharsets;
76
import java.util.Date;
87

98
/**
@@ -16,10 +15,7 @@ public class TimeServerHandler extends ChannelInboundHandlerAdapter {
1615

1716
@Override
1817
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
19-
ByteBuf byteBuf = (ByteBuf) msg;
20-
byte[] req = new byte[byteBuf.readableBytes()];
21-
byteBuf.readBytes(req);
22-
String body = new String(req, StandardCharsets.UTF_8).substring(0, req.length - System.getProperty(Constant.SEP).length());
18+
String body = (String) msg;
2319
System.out.println("receive:" + body + ",counter:" + ++counter);
2420
String time = Constant.REQ.equalsIgnoreCase(body) ? new Date().toString() : Constant.BAD_REQ;
2521
time = time + System.getProperty(Constant.SEP);
@@ -29,6 +25,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
2925

3026
@Override
3127
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
28+
cause.printStackTrace();
3229
ctx.close();
3330
}
3431
}

0 commit comments

Comments
 (0)