- 浏览: 5964 次
- 性别:
- 来自: 厦门
最新评论
一:TCP粘包拆包简介
TCP是个"流"协议,底层并不了解上次业务数据的具体含义,它是根据TCP缓冲区的实际情况对数据包进行划分,所以在数据传输过程中一个完整的数据包可能会被TCP拆分成多个包进行发送,也有可能把多个小的数据包封装成一个大的数据包发送,这就是TCP的粘包拆包问题。
二:粘包拆包的解决思路
由于底层的TCP无法理解上层的业务数据,所以解决思路只能通过上层的应用协议栈设计来解决,目前主流协议的解决方案有:
(1)消息定长,如长度200个字节,不够的补空格;
(2)在数据包结尾添加回车换行符进行分割,如FTP协议;
(3)将消息分为消息头和消息体,消息头中包含消息的总长度。
三:Netty关于粘包拆包的解决方案
对于数据量较小时并不会出现粘包问题,但当数据报文较大时,或者压力一旦上来,就会存在粘包拆包问题。下面将模拟粘包拆包案例。
1、TimeServer
2、TimeServerHandler
3、TimeClient
4、TimeClienHandler
运行结果如下:
服务器端:
the time server receive order :QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORD; the counter is 1
the time server receive order :
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER; the counter is 2
客户端:
now is :BAD ORDER
BAD ORDER
; the counter is :1
测试说明:
如果不出现粘包问题,服务器端预期收到的应该是:
the time server receive order :QUERY TIME ORDER ; the counter is :1
这样的100条消息,客户端预期收到的应该是:
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :1
这样的100条消息,但实际测试确出现了上述的测试结果,说明在传输过程中出现了粘包问题。下面利用Netty提供的编码器解决TCP粘包拆包问题(LineBasedFrameDecoder&&StringDecoder)。
1、TimeServer
2、TimeServerHandler
3、TimeClient
4、TimeClienHandler
运行结果如下:
服务器端:
the time server receive order :QUERY TIME ORDER ; the counter is :1
the time server receive order :QUERY TIME ORDER ; the counter is :2
the time server receive order :QUERY TIME ORDER ; the counter is :3
the time server receive order :QUERY TIME ORDER ; the counter is :4
the time server receive order :QUERY TIME ORDER ; the counter is :5
the time server receive order :QUERY TIME ORDER ; the counter is :6
the time server receive order :QUERY TIME ORDER ; the counter is :7
the time server receive order :QUERY TIME ORDER ; the counter is :8
the time server receive order :QUERY TIME ORDER ; the counter is :9
the time server receive order :QUERY TIME ORDER ; the counter is :10
the time server receive order :QUERY TIME ORDER ; the counter is :11
the time server receive order :QUERY TIME ORDER ; the counter is :12
the time server receive order :QUERY TIME ORDER ; the counter is :13
the time server receive order :QUERY TIME ORDER ; the counter is :14
the time server receive order :QUERY TIME ORDER ; the counter is :15
the time server receive order :QUERY TIME ORDER ; the counter is :16
the time server receive order :QUERY TIME ORDER ; the counter is :17
the time server receive order :QUERY TIME ORDER ; the counter is :18
the time server receive order :QUERY TIME ORDER ; the counter is :19
the time server receive order :QUERY TIME ORDER ; the counter is :20
the time server receive order :QUERY TIME ORDER ; the counter is :21
the time server receive order :QUERY TIME ORDER ; the counter is :22
the time server receive order :QUERY TIME ORDER ; the counter is :23
the time server receive order :QUERY TIME ORDER ; the counter is :24
the time server receive order :QUERY TIME ORDER ; the counter is :25
the time server receive order :QUERY TIME ORDER ; the counter is :26
the time server receive order :QUERY TIME ORDER ; the counter is :27
the time server receive order :QUERY TIME ORDER ; the counter is :28
the time server receive order :QUERY TIME ORDER ; the counter is :29
the time server receive order :QUERY TIME ORDER ; the counter is :30
the time server receive order :QUERY TIME ORDER ; the counter is :31
the time server receive order :QUERY TIME ORDER ; the counter is :32
the time server receive order :QUERY TIME ORDER ; the counter is :33
the time server receive order :QUERY TIME ORDER ; the counter is :34
the time server receive order :QUERY TIME ORDER ; the counter is :35
the time server receive order :QUERY TIME ORDER ; the counter is :36
the time server receive order :QUERY TIME ORDER ; the counter is :37
the time server receive order :QUERY TIME ORDER ; the counter is :38
the time server receive order :QUERY TIME ORDER ; the counter is :39
the time server receive order :QUERY TIME ORDER ; the counter is :40
the time server receive order :QUERY TIME ORDER ; the counter is :41
the time server receive order :QUERY TIME ORDER ; the counter is :42
the time server receive order :QUERY TIME ORDER ; the counter is :43
the time server receive order :QUERY TIME ORDER ; the counter is :44
the time server receive order :QUERY TIME ORDER ; the counter is :45
the time server receive order :QUERY TIME ORDER ; the counter is :46
the time server receive order :QUERY TIME ORDER ; the counter is :47
the time server receive order :QUERY TIME ORDER ; the counter is :48
the time server receive order :QUERY TIME ORDER ; the counter is :49
the time server receive order :QUERY TIME ORDER ; the counter is :50
the time server receive order :QUERY TIME ORDER ; the counter is :51
the time server receive order :QUERY TIME ORDER ; the counter is :52
the time server receive order :QUERY TIME ORDER ; the counter is :53
the time server receive order :QUERY TIME ORDER ; the counter is :54
the time server receive order :QUERY TIME ORDER ; the counter is :55
the time server receive order :QUERY TIME ORDER ; the counter is :56
the time server receive order :QUERY TIME ORDER ; the counter is :57
the time server receive order :QUERY TIME ORDER ; the counter is :58
the time server receive order :QUERY TIME ORDER ; the counter is :59
the time server receive order :QUERY TIME ORDER ; the counter is :60
the time server receive order :QUERY TIME ORDER ; the counter is :61
the time server receive order :QUERY TIME ORDER ; the counter is :62
the time server receive order :QUERY TIME ORDER ; the counter is :63
the time server receive order :QUERY TIME ORDER ; the counter is :64
the time server receive order :QUERY TIME ORDER ; the counter is :65
the time server receive order :QUERY TIME ORDER ; the counter is :66
the time server receive order :QUERY TIME ORDER ; the counter is :67
the time server receive order :QUERY TIME ORDER ; the counter is :68
the time server receive order :QUERY TIME ORDER ; the counter is :69
the time server receive order :QUERY TIME ORDER ; the counter is :70
the time server receive order :QUERY TIME ORDER ; the counter is :71
the time server receive order :QUERY TIME ORDER ; the counter is :72
the time server receive order :QUERY TIME ORDER ; the counter is :73
the time server receive order :QUERY TIME ORDER ; the counter is :74
the time server receive order :QUERY TIME ORDER ; the counter is :75
the time server receive order :QUERY TIME ORDER ; the counter is :76
the time server receive order :QUERY TIME ORDER ; the counter is :77
the time server receive order :QUERY TIME ORDER ; the counter is :78
the time server receive order :QUERY TIME ORDER ; the counter is :79
the time server receive order :QUERY TIME ORDER ; the counter is :80
the time server receive order :QUERY TIME ORDER ; the counter is :81
the time server receive order :QUERY TIME ORDER ; the counter is :82
the time server receive order :QUERY TIME ORDER ; the counter is :83
the time server receive order :QUERY TIME ORDER ; the counter is :84
the time server receive order :QUERY TIME ORDER ; the counter is :85
the time server receive order :QUERY TIME ORDER ; the counter is :86
the time server receive order :QUERY TIME ORDER ; the counter is :87
the time server receive order :QUERY TIME ORDER ; the counter is :88
the time server receive order :QUERY TIME ORDER ; the counter is :89
the time server receive order :QUERY TIME ORDER ; the counter is :90
the time server receive order :QUERY TIME ORDER ; the counter is :91
the time server receive order :QUERY TIME ORDER ; the counter is :92
the time server receive order :QUERY TIME ORDER ; the counter is :93
the time server receive order :QUERY TIME ORDER ; the counter is :94
the time server receive order :QUERY TIME ORDER ; the counter is :95
the time server receive order :QUERY TIME ORDER ; the counter is :96
the time server receive order :QUERY TIME ORDER ; the counter is :97
the time server receive order :QUERY TIME ORDER ; the counter is :98
the time server receive order :QUERY TIME ORDER ; the counter is :99
the time server receive order :QUERY TIME ORDER ; the counter is :100
客户端:
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :1
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :2
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :3
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :4
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :5
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :6
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :7
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :8
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :9
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :10
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :11
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :12
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :13
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :14
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :15
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :16
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :17
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :18
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :19
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :20
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :21
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :22
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :23
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :24
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :25
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :26
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :27
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :28
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :29
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :30
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :31
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :32
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :33
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :34
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :35
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :36
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :37
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :38
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :39
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :40
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :41
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :42
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :43
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :44
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :45
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :46
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :47
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :48
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :49
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :50
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :51
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :52
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :53
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :54
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :55
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :56
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :57
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :58
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :59
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :60
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :61
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :62
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :63
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :64
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :65
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :66
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :67
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :68
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :69
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :70
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :71
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :72
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :73
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :74
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :75
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :76
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :77
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :78
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :79
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :80
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :81
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :82
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :83
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :84
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :85
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :86
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :87
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :88
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :89
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :90
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :91
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :92
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :93
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :94
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :95
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :96
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :97
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :98
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :99
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :100
测试说明:
测试结果正确的收到了消息,说明LineBasedFrameDecoder和StringDecoder成功解决了TCP的粘包问题。
除了上述的解码器,Netty还提供了多种支持TCP粘包拆包的解码器,用来满足不同的用户需求,在接下来的文章中将继续讲解这些解码器。
TCP是个"流"协议,底层并不了解上次业务数据的具体含义,它是根据TCP缓冲区的实际情况对数据包进行划分,所以在数据传输过程中一个完整的数据包可能会被TCP拆分成多个包进行发送,也有可能把多个小的数据包封装成一个大的数据包发送,这就是TCP的粘包拆包问题。
二:粘包拆包的解决思路
由于底层的TCP无法理解上层的业务数据,所以解决思路只能通过上层的应用协议栈设计来解决,目前主流协议的解决方案有:
(1)消息定长,如长度200个字节,不够的补空格;
(2)在数据包结尾添加回车换行符进行分割,如FTP协议;
(3)将消息分为消息头和消息体,消息头中包含消息的总长度。
三:Netty关于粘包拆包的解决方案
对于数据量较小时并不会出现粘包问题,但当数据报文较大时,或者压力一旦上来,就会存在粘包拆包问题。下面将模拟粘包拆包案例。
1、TimeServer
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; public class TimeServer { public void bind(int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler()); ChannelFuture f = bootstrap.bind(port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { // TODO: handle exception } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) { new TimeServer().bind(8080); } } class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel arg0) throws Exception { // TODO Auto-generated method stub arg0.pipeline().addLast(new TimeServerHandler()); } }
2、TimeServerHandler
import java.util.Date; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; public class TimeServerHandler extends ChannelHandlerAdapter{ private int counter; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8").substring(0, req.length - System.getProperty("line.separator").length()); System.out.println("the time server receive order :" + body+"; the counter is "+ ++counter); String currentyTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; currentyTime = currentyTime + System.getProperty("line.separator"); ByteBuf copiedBuffer = Unpooled.copiedBuffer(currentyTime.getBytes()); ctx.write(copiedBuffer); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub ctx.close(); } }
3、TimeClient
import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class TimeClient { public void connect(String host, int port) { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel s) throws Exception { s.pipeline().addLast(new TimeClienHandler()); }; }); ChannelFuture f = bootstrap.connect(host, port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { // TODO: handle exception } finally { group.shutdownGracefully(); } } public static void main(String[] args) { new TimeClient().connect("127.0.0.1", 8080); } }
4、TimeClienHandler
import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; public class TimeClienHandler extends ChannelHandlerAdapter { private int counter; private final byte[] req; // private final ByteBuf buf; public TimeClienHandler() { req = ("QUERY TIME ORDER" + System.getProperty("line.separator")).getBytes(); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub ByteBuf message = null; for (int i = 0; i < 100; i++) { message = Unpooled.buffer(req.length); message.writeBytes(req); ctx.writeAndFlush(message); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8"); // String body = (String) msg; System.out.println("now is :" + body + "; the counter is :" + ++counter); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub ctx.close(); } }
运行结果如下:
服务器端:
the time server receive order :QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORD; the counter is 1
the time server receive order :
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER
QUERY TIME ORDER; the counter is 2
客户端:
now is :BAD ORDER
BAD ORDER
; the counter is :1
测试说明:
如果不出现粘包问题,服务器端预期收到的应该是:
the time server receive order :QUERY TIME ORDER ; the counter is :1
这样的100条消息,客户端预期收到的应该是:
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :1
这样的100条消息,但实际测试确出现了上述的测试结果,说明在传输过程中出现了粘包问题。下面利用Netty提供的编码器解决TCP粘包拆包问题(LineBasedFrameDecoder&&StringDecoder)。
1、TimeServer
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.LineBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; /** * 编码器LineBasedFrameDecoder && StringDecoder */ public class TimeServer { public void bind(int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler()); ChannelFuture f = bootstrap.bind(port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { // TODO: handle exception } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) { new TimeServer().bind(8080); } } class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel arg0) throws Exception { // TODO Auto-generated method stub arg0.pipeline().addLast(new LineBasedFrameDecoder(1024)); arg0.pipeline().addLast(new StringDecoder()); arg0.pipeline().addLast(new TimeHandler()); } }
2、TimeServerHandler
import java.util.Date; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; public class TimeServerHandler extends ChannelHandlerAdapter { private int counter; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub String s = (String) msg; System.out.println("the time server receive order :" + s + " ; the counter is :" + ++counter); String currentyTime = "QUERY TIME ORDER".equalsIgnoreCase(s) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; currentyTime = currentyTime + System.getProperty("line.separator"); ByteBuf copiedBuffer = Unpooled.copiedBuffer(currentyTime.getBytes()); ctx.write(copiedBuffer); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub ctx.close(); } }
3、TimeClient
import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.LineBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; public class TimeClient { public void connect(String host, int port) { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel s) throws Exception { s.pipeline().addLast(new LineBasedFrameDecoder(1024)); s.pipeline().addLast(new StringDecoder()); s.pipeline().addLast(new TimeClienHandler()); }; }); ChannelFuture f = bootstrap.connect(host, port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { // TODO: handle exception } finally { group.shutdownGracefully(); } } public static void main(String[] args) { new TimeClient().connect("127.0.0.1", 8080); } }
4、TimeClienHandler
import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; public class TimeClienHandler extends ChannelHandlerAdapter { private int counter; private final byte[] req; public TimeClienHandler() { req = ("QUERY TIME ORDER" + System.getProperty("line.separator")).getBytes(); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method stub ByteBuf message = null; for (int i = 0; i < 100; i++) { message = Unpooled.buffer(req.length); message.writeBytes(req); ctx.writeAndFlush(message); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub String body = (String) msg; System.out.println("now is :" + body + "; the counter is :" + ++counter); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub ctx.close(); } }
运行结果如下:
服务器端:
the time server receive order :QUERY TIME ORDER ; the counter is :1
the time server receive order :QUERY TIME ORDER ; the counter is :2
the time server receive order :QUERY TIME ORDER ; the counter is :3
the time server receive order :QUERY TIME ORDER ; the counter is :4
the time server receive order :QUERY TIME ORDER ; the counter is :5
the time server receive order :QUERY TIME ORDER ; the counter is :6
the time server receive order :QUERY TIME ORDER ; the counter is :7
the time server receive order :QUERY TIME ORDER ; the counter is :8
the time server receive order :QUERY TIME ORDER ; the counter is :9
the time server receive order :QUERY TIME ORDER ; the counter is :10
the time server receive order :QUERY TIME ORDER ; the counter is :11
the time server receive order :QUERY TIME ORDER ; the counter is :12
the time server receive order :QUERY TIME ORDER ; the counter is :13
the time server receive order :QUERY TIME ORDER ; the counter is :14
the time server receive order :QUERY TIME ORDER ; the counter is :15
the time server receive order :QUERY TIME ORDER ; the counter is :16
the time server receive order :QUERY TIME ORDER ; the counter is :17
the time server receive order :QUERY TIME ORDER ; the counter is :18
the time server receive order :QUERY TIME ORDER ; the counter is :19
the time server receive order :QUERY TIME ORDER ; the counter is :20
the time server receive order :QUERY TIME ORDER ; the counter is :21
the time server receive order :QUERY TIME ORDER ; the counter is :22
the time server receive order :QUERY TIME ORDER ; the counter is :23
the time server receive order :QUERY TIME ORDER ; the counter is :24
the time server receive order :QUERY TIME ORDER ; the counter is :25
the time server receive order :QUERY TIME ORDER ; the counter is :26
the time server receive order :QUERY TIME ORDER ; the counter is :27
the time server receive order :QUERY TIME ORDER ; the counter is :28
the time server receive order :QUERY TIME ORDER ; the counter is :29
the time server receive order :QUERY TIME ORDER ; the counter is :30
the time server receive order :QUERY TIME ORDER ; the counter is :31
the time server receive order :QUERY TIME ORDER ; the counter is :32
the time server receive order :QUERY TIME ORDER ; the counter is :33
the time server receive order :QUERY TIME ORDER ; the counter is :34
the time server receive order :QUERY TIME ORDER ; the counter is :35
the time server receive order :QUERY TIME ORDER ; the counter is :36
the time server receive order :QUERY TIME ORDER ; the counter is :37
the time server receive order :QUERY TIME ORDER ; the counter is :38
the time server receive order :QUERY TIME ORDER ; the counter is :39
the time server receive order :QUERY TIME ORDER ; the counter is :40
the time server receive order :QUERY TIME ORDER ; the counter is :41
the time server receive order :QUERY TIME ORDER ; the counter is :42
the time server receive order :QUERY TIME ORDER ; the counter is :43
the time server receive order :QUERY TIME ORDER ; the counter is :44
the time server receive order :QUERY TIME ORDER ; the counter is :45
the time server receive order :QUERY TIME ORDER ; the counter is :46
the time server receive order :QUERY TIME ORDER ; the counter is :47
the time server receive order :QUERY TIME ORDER ; the counter is :48
the time server receive order :QUERY TIME ORDER ; the counter is :49
the time server receive order :QUERY TIME ORDER ; the counter is :50
the time server receive order :QUERY TIME ORDER ; the counter is :51
the time server receive order :QUERY TIME ORDER ; the counter is :52
the time server receive order :QUERY TIME ORDER ; the counter is :53
the time server receive order :QUERY TIME ORDER ; the counter is :54
the time server receive order :QUERY TIME ORDER ; the counter is :55
the time server receive order :QUERY TIME ORDER ; the counter is :56
the time server receive order :QUERY TIME ORDER ; the counter is :57
the time server receive order :QUERY TIME ORDER ; the counter is :58
the time server receive order :QUERY TIME ORDER ; the counter is :59
the time server receive order :QUERY TIME ORDER ; the counter is :60
the time server receive order :QUERY TIME ORDER ; the counter is :61
the time server receive order :QUERY TIME ORDER ; the counter is :62
the time server receive order :QUERY TIME ORDER ; the counter is :63
the time server receive order :QUERY TIME ORDER ; the counter is :64
the time server receive order :QUERY TIME ORDER ; the counter is :65
the time server receive order :QUERY TIME ORDER ; the counter is :66
the time server receive order :QUERY TIME ORDER ; the counter is :67
the time server receive order :QUERY TIME ORDER ; the counter is :68
the time server receive order :QUERY TIME ORDER ; the counter is :69
the time server receive order :QUERY TIME ORDER ; the counter is :70
the time server receive order :QUERY TIME ORDER ; the counter is :71
the time server receive order :QUERY TIME ORDER ; the counter is :72
the time server receive order :QUERY TIME ORDER ; the counter is :73
the time server receive order :QUERY TIME ORDER ; the counter is :74
the time server receive order :QUERY TIME ORDER ; the counter is :75
the time server receive order :QUERY TIME ORDER ; the counter is :76
the time server receive order :QUERY TIME ORDER ; the counter is :77
the time server receive order :QUERY TIME ORDER ; the counter is :78
the time server receive order :QUERY TIME ORDER ; the counter is :79
the time server receive order :QUERY TIME ORDER ; the counter is :80
the time server receive order :QUERY TIME ORDER ; the counter is :81
the time server receive order :QUERY TIME ORDER ; the counter is :82
the time server receive order :QUERY TIME ORDER ; the counter is :83
the time server receive order :QUERY TIME ORDER ; the counter is :84
the time server receive order :QUERY TIME ORDER ; the counter is :85
the time server receive order :QUERY TIME ORDER ; the counter is :86
the time server receive order :QUERY TIME ORDER ; the counter is :87
the time server receive order :QUERY TIME ORDER ; the counter is :88
the time server receive order :QUERY TIME ORDER ; the counter is :89
the time server receive order :QUERY TIME ORDER ; the counter is :90
the time server receive order :QUERY TIME ORDER ; the counter is :91
the time server receive order :QUERY TIME ORDER ; the counter is :92
the time server receive order :QUERY TIME ORDER ; the counter is :93
the time server receive order :QUERY TIME ORDER ; the counter is :94
the time server receive order :QUERY TIME ORDER ; the counter is :95
the time server receive order :QUERY TIME ORDER ; the counter is :96
the time server receive order :QUERY TIME ORDER ; the counter is :97
the time server receive order :QUERY TIME ORDER ; the counter is :98
the time server receive order :QUERY TIME ORDER ; the counter is :99
the time server receive order :QUERY TIME ORDER ; the counter is :100
客户端:
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :1
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :2
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :3
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :4
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :5
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :6
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :7
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :8
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :9
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :10
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :11
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :12
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :13
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :14
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :15
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :16
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :17
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :18
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :19
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :20
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :21
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :22
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :23
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :24
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :25
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :26
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :27
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :28
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :29
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :30
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :31
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :32
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :33
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :34
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :35
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :36
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :37
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :38
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :39
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :40
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :41
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :42
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :43
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :44
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :45
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :46
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :47
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :48
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :49
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :50
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :51
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :52
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :53
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :54
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :55
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :56
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :57
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :58
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :59
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :60
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :61
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :62
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :63
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :64
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :65
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :66
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :67
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :68
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :69
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :70
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :71
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :72
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :73
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :74
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :75
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :76
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :77
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :78
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :79
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :80
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :81
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :82
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :83
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :84
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :85
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :86
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :87
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :88
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :89
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :90
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :91
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :92
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :93
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :94
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :95
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :96
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :97
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :98
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :99
now is :Mon Sep 21 10:33:17 CST 2015; the counter is :100
测试说明:
测试结果正确的收到了消息,说明LineBasedFrameDecoder和StringDecoder成功解决了TCP的粘包问题。
除了上述的解码器,Netty还提供了多种支持TCP粘包拆包的解码器,用来满足不同的用户需求,在接下来的文章中将继续讲解这些解码器。
相关推荐
使用Netty解决TCP粘包和拆包问题过程详解 Netty是一个流行的Java网络编程框架,提供了简洁、灵活的API来处理网络编程的各种问题。其中,解决TCP粘包和拆包问题是Netty的一个重要应用场景。本文将详细介绍使用Netty...
### Netty精粹之TCP粘包拆包问题详解 #### 一、引言 在网络通信领域,尤其是在基于TCP协议的应用程序开发中,经常会遇到“粘包”和“拆包”的问题。这些问题虽然属于较为底层的技术细节,但对于保障数据传输的准确...
注:下载前请查看本人博客文章,看是否...里面包含模拟TCP客户端发送报文工具,硬件厂商提供的协议,服务端(springboot+netty)解析报文源码,源码里整合了redis,不需要可自行删除,如有需要客户端代码,可联系我。
springboot整合netty的实践操作(解决粘包拆包问题)
Netty 提供了多种解决 TCP 粘包/拆包问题的方法,例如使用 LineBasedFrameDecoder、StringDecoder 等编解码器来解决半包读写问题。在使用 Netty 时,可以通过添加编解码器来解决 TCP 粘包/拆包问题,以确保数据传输...
Netty 粘包拆包问题解决方案 Netty 是一个基于 Java 的网络编程框架,它提供了一个便捷的方式来处理网络数据的读写操作。然而,在使用 Netty 进行网络编程时,经常会遇到粘包和拆包的问题。所谓粘包和拆包,就是指...
本教程将深入探讨如何在Unity中实现Socket通信,包括TCP连接、粘包/拆包问题的解决方案。 一、TCP连接基础 TCP(Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议。在Unity中...
本文将深入探讨Netty中解决拆包粘包问题的策略,以及客户端断线重连的实现方式。 首先,我们需要理解什么是“拆包”和“粘包”。在TCP/IP通信中,由于TCP协议的流式传输特性,数据可能会被分片或者合并,这就可能...
下面我们将深入探讨Netty如何搭建TCP服务以及它如何解决粘包和拆包的问题。 首先,TCP本身并不区分消息边界,它只是一个字节流的传输层协议,这意味着连续发送的多个数据包可能会被合并成一个大的数据包发送,或者...
### 粘包和拆包及Netty解决方案 #### 一、粘包和拆包问题概述 在计算机网络通信中,特别是在使用TCP/IP协议栈进行数据传输的过程中,常常会遇到粘包和拆包的问题。这类问题主要出现在客户端和服务端通过TCP连接...
总的来说,Netty提供的解码器机制使得开发者无需深入理解TCP底层细节,也能有效地解决粘包和拆包问题,提高了网络通信的可靠性和效率。通过选择合适的解码器并合理配置,我们可以构建出健壮的网络应用。
在计算机网络编程中,"拆包"和"粘包"是TCP协议中常见的问题,尤其在C++服务器开发中,理解并处理好这两个概念对于构建高效稳定的网络服务至关重要。TCP是一种面向连接的、可靠的传输层协议,它通过流式传输确保数据...
TCP的粘包和拆包是网络编程中遇到的常见问题,尤其在基于TCP协议的通讯中,如RPC框架和Netty等。TCP是一种面向字节流的协议,它没有明确的数据包边界,这可能导致发送的数据在接收端看起来像是被粘在一起或是被拆...
在TCP网络编程中,粘包和拆包是常见的问题,主要由于TCP协议的特性——它是一个面向流的协议,不保证数据包的边界。在Netty框架中,这些问题需要通过特定的策略和组件来解决。 TCP粘包和拆包的产生原因是多方面的,...
**TCP的拆包粘包问题**:TCP是面向字节流的协议,不区分消息边界,可能导致数据接收方无法准确解析消息。Netty通过提供一系列的编解码器,如LengthFieldBasedFrameDecoder,解决了这个问题。它可以根据前导长度字段...
本实例是《Netty 粘包/半包原理与拆包实战》 一文的源代码工程。 大家好,我是作者尼恩。 在前面的文章中,完成了一个高性能的 Java 聊天程序,尼恩已经再一次的进行了通讯协议的选择。放弃了大家非常熟悉的json ...
然而,TCP在处理数据传输时,可能会出现“粘包”现象,这是开发者需要理解和解决的一个关键问题。 粘包是指在一个TCP连接中,发送方发送的多个数据包在接收方可能被合并成一个大的数据包进行接收,或者接收方可能将...
在TCP/IP协议中,由于数据的传输是以字节流的方式进行,如果不对数据进行适当的处理,就可能出现“粘包”和“拆包”的问题。本文将深入探讨Netty中如何处理这两个问题,并通过源码分析来理解其内部机制。 “粘包”...
93个netty高并发全面的教学视频下载,每个视频在400-700M,一到两个小时时长的视频,无机器码和解压密码,下载下来的就是MP4格式...91_Netty自定义协议与TCP粘包拆包问题解决之道;92_精通并发与Netty课程总结与展望