概述
TCP是面向连接、面向流的,可以提供高可靠性服务。收发两端(客户端和服务器端)都要有一一成对的socket。因此,发送端为了将多个发给接收端的包更有效的发给对方,使用了优化方法,将多次间隔较小且数据量小的数据合成为一个大的数据块,然后进行封包。这样做虽然提高了效率,但是接收端就很难分辨出接收到的包是否为一个完整的数据包了。因为面向流的通信时无消息保护边界的。
由于TCP无消息保护边界,需要在接收端处理消息边界问题。因此会有拆包、粘包问题,如下图
说明:
假设客户端分别发送了两个数据包D1和D2给服务端,由于服务端一次读取到的字节数是不确定的,因此可能存在以下四种情况:
- 服务器端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包、拆包问题
- 服务器端一次接受到了两个数据包,D1和D2粘合在一起,称为TCP粘包
- 服务器端分两次读取到了数据包,第一次读取到了完整的D1包和D2包的部分内容,第二次读取到了D2包的剩余内容,这称之为TCP拆包。
- 服务器端分两次读取到了数据包,第一次读取到了D1包的部分内容,第二次读取到了D1包的剩余部分内容和完整的D2包
代码演示TCP粘包、拆包现象
服务器端:
package com.wojiushiwo.tcp.packingunpacking;
import com.wojiushiwo.codec.CustomCodecServerHandler;
import com.wojiushiwo.codec.LongDecoder;
import com.wojiushiwo.codec.LongEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* Created by myk
* 2020/1/29 下午8:26
*/
public class PackingUnpackingServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new PackingUnpackingServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(8091).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
//handler
package com.wojiushiwo.tcp.packingunpacking;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* Created by myk
* 2020/1/29 下午8:27
*/
public class PackingUnpackingServerHandler extends ChannelInboundHandlerAdapter {
private int count = 0;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.out.println("from client:" + buf.toString(CharsetUtil.UTF_8));
System.out.println("服务器端接收到的消息量=" + (++this.count));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端代码
package com.wojiushiwo.tcp.packingunpacking;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* Created by myk
* 2020/1/29 下午8:26
*/
public class PackingUnpackingClient {
public static void main(String[] args) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new PackingUnpackingClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8091).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}
//handler
package com.wojiushiwo.tcp.packingunpacking;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* Created by myk
* 2020/1/29 下午8:27
*/
public class PackingUnpackingClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//使用客户端发送5次消息
for (int i = 0; i < 5; i++) {
ctx.writeAndFlush(Unpooled.copiedBuffer("from client" + (i + 1) + " ", CharsetUtil.UTF_8));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}
客户端循环5次 向服务器端发送消息,通过查看服务器端接收到的消息完整程度即可判断粘包、拆包现象
启动了服务端,并且启动了三个客户端,输出结果:
//客户端1启动后服务器端输出
from client:from client1 from client2 from client3 from client4 from client5
服务器端接收到的消息量=1
//客户端2启动后服务器端输出
from client:from client1
服务器端接收到的消息量=1
from client:from client2
服务器端接收到的消息量=2
from client:from client3 from client4 from client5
服务器端接收到的消息量=3
// 客户端3启动后服务器端输出
from client:from client1
服务器端接收到的消息量=1
from client:from client2
服务器端接收到的消息量=2
from client:from client3
服务器端接收到的消息量=3
from client:from client4
服务器端接收到的消息量=4
from client:from client5
服务器端接收到的消息量=5
可以发现,输出结果中粘包、拆包等情况都发生了。
解决方案
只要确定了服务器端每次传输的数据的长度,那么服务器端一次读取到的就是完整的数据,就可以避免TCP拆包、粘包问题。
通常使用自定义协议+编解码器来解决
解决方案示例:
服务器端:
package com.wojiushiwo.tcp.protocol;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* Created by myk
* 2020/1/29 下午9:53
*/
public class Server {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("encoder",new PackingUnPackingEncoder());
pipeline.addLast("decoder",new PackingUnpackingDecoder());
pipeline.addLast(new ServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(8091).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
//handler
package com.wojiushiwo.tcp.protocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
/**
* Created by myk
* 2020/1/29 下午9:53
*/
public class ServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {
System.out.println("from client[length:" + msg.getLength() + "内容:" + new String(msg.getContent(), CharsetUtil.UTF_8) + "]");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端代码
package com.wojiushiwo.tcp.protocol;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* Created by myk
* 2020/1/29 下午9:53
*/
public class Client {
public static void main(String[] args) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("encoder", new PackingUnPackingEncoder());
pipeline.addLast("decoder", new PackingUnpackingDecoder());
pipeline.addLast(new ClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8091).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}
//handler
package com.wojiushiwo.tcp.protocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* Created by myk
* 2020/1/29 下午9:53
*/
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0; i < 10; i++) {
MessageProtocol messageProtocol = new MessageProtocol();
String content = "hello,packing&unpacking," + (i + 1);
messageProtocol.setContent(content.getBytes());
messageProtocol.setLength(content.getBytes().length);
ctx.writeAndFlush(messageProtocol);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
自定义对象及编解码器
package com.wojiushiwo.tcp.protocol;
import lombok.Data;
/**
* Created by myk
* 2020/1/29 下午9:47
*/
@Data
public class MessageProtocol {
//保存每次数据发送的长度
private int length;
//保存每次发送数据内存
private byte[] content;
}
//编解码器
package com.wojiushiwo.tcp.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
* Created by myk
* 2020/1/29 下午9:48
*/
public class PackingUnPackingEncoder extends MessageToByteEncoder<MessageProtocol> {
@Override
protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
out.writeInt(msg.getLength());
out.writeBytes(msg.getContent());
}
}
package com.wojiushiwo.tcp.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import java.util.List;
/**
* Created by myk
* 2020/1/29 下午9:49
*/
public class PackingUnpackingDecoder extends ReplayingDecoder<Void> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
//这里的in代表的类型是ReplayingDecoderByteBuf
int length = in.readInt();
byte[] content = new byte[length];
in.readBytes(content);
MessageProtocol messageProtocol = new MessageProtocol();
messageProtocol.setContent(content);
messageProtocol.setLength(length);
//封装成 MessageProtocol 对象,放入 out, 传递下一个 handler 业务处理
out.add(messageProtocol);
}
}
服务器端启动后,启动了三个客户端,输出结果无误
//客户端1启动后,服务器端输出
from client[length:25内容:hello,packing&unpacking,1]
from client[length:25内容:hello,packing&unpacking,2]
from client[length:25内容:hello,packing&unpacking,3]
from client[length:25内容:hello,packing&unpacking,4]
from client[length:25内容:hello,packing&unpacking,5]
from client[length:25内容:hello,packing&unpacking,6]
from client[length:25内容:hello,packing&unpacking,7]
from client[length:25内容:hello,packing&unpacking,8]
from client[length:25内容:hello,packing&unpacking,9]
from client[length:26内容:hello,packing&unpacking,10]
//客户端2启动后,服务器端输出
from client[length:25内容:hello,packing&unpacking,1]
from client[length:25内容:hello,packing&unpacking,2]
from client[length:25内容:hello,packing&unpacking,3]
from client[length:25内容:hello,packing&unpacking,4]
from client[length:25内容:hello,packing&unpacking,5]
from client[length:25内容:hello,packing&unpacking,6]
from client[length:25内容:hello,packing&unpacking,7]
from client[length:25内容:hello,packing&unpacking,8]
from client[length:25内容:hello,packing&unpacking,9]
from client[length:26内容:hello,packing&unpacking,10]
//客户端3启动后,服务器端输出
from client[length:25内容:hello,packing&unpacking,1]
from client[length:25内容:hello,packing&unpacking,2]
from client[length:25内容:hello,packing&unpacking,3]
from client[length:25内容:hello,packing&unpacking,4]
from client[length:25内容:hello,packing&unpacking,5]
from client[length:25内容:hello,packing&unpacking,6]
from client[length:25内容:hello,packing&unpacking,7]
from client[length:25内容:hello,packing&unpacking,8]
from client[length:25内容:hello,packing&unpacking,9]
from client[length:26内容:hello,packing&unpacking,10]
通过输出结果,可以看出粘包、拆包问题得到了解决。