概述
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]
通過輸出結果,可以看出粘包、拆包問題得到了解決。