![](/img/trans.png)
[英]Sending TCP packets via Netty, Netty is dividing the data into different packets?
[英]Netty: Why different packets are connected together as an request in the server?
這是Netty客戶端中唯一的處理程序,我向服務器發送了3個數據包。
@Sharable
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.copiedBuffer("1", CharsetUtil.UTF_8));
ctx.writeAndFlush(Unpooled.copiedBuffer("2", CharsetUtil.UTF_8));
ctx.writeAndFlush(Unpooled.copiedBuffer("3", CharsetUtil.UTF_8))
.addListener(ChannelFutureListener.CLOSE);
}
}
在服務器處理,我只是單獨打印出來,預計3次1
, 2
和3
,但123
實際上。 發生了什么? 它們不是不同的包裝嗎?
@Sharable
public class ServerHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {
System.out.println(in.toString(CharsetUtil.UTF_8));
}
}
TCP / IP協議(您可能正在服務器中使用)是基於流的。 這意味着基於流的傳輸的緩沖區不是數據包隊列,而是字節隊列。 因此,取決於操作系統如何發送數據-作為單獨的數據包還是合並了所有數據的1個數據包。
您有3個選項,或者添加一些分隔符,或者發送固定長度的數據包,或者將數據包的大小附加到消息中。
這是 Netty文檔中的更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.