![](/img/trans.png)
[英]Message is not received by client in Netty when there are more number of messages sent to Server
[英]Is it possible that the messages client received is out of order as the netty server write
for (int i = 1; i <= 100; i++) {
ctx.writeAndFlush(Unpooled.copiedBuffer(Integer.toString(i).getBytes(Charsets.US_ASCII)));
}
ctx.writeAndFlush(Unpooled.copiedBuffer("ABCD".getBytes(Charsets.US_ASCII))).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
ctx.channel().close();
}
});
我在netty服務器處理程序的channelRead()mehtod中寫這個,一旦服務器收到請求,它就會將“12345 ... 100ABCD”響應回客戶端。
據我所知,從netty服務器收到的消息客戶端的順序始終是“12345 ... 100ABCD”。
我不知道這只是偶然的嗎? 也許有時它會是“32451 ... ABCD100”(服務器寫入順序之外)?
是否有可能服務器執行
clientChannel.writeAndFlush(msg1);
clientChannel.writeAndFlush(msg2);
clientChannel.writeAndFlush(msg3);
但是客戶端收到了msg2-msg1-msg3或msg3-msg1-msg2但沒有寫入命令msg1-msg2-msg3
在netty項目的代理示例中, https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example/proxy
HexDumpProxyBackendHandler寫道:
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
ctx.channel().read();
} else {
future.channel().close();
}
}
});
}
它確保僅在wirteAndFlush()操作完成時觸發next channelRead()(即channelRead()中的inboundChannel.writeAndFlush(msg))。
那么在listener中編寫ctx.channel()。read()並在future.isSuccess()時執行它的目的是什么? 是不是要確保以正確的順序接收寫入客戶端的消息?
如果我改成它
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
inboundChannel.writeAndFlush(msg);
ctx.channel().read();
}
它會引起一些問題嗎?
不,這是不可能的。 TCP確保了這一點。
正如EJP所述,任何一項技術都應該保證訂購。 示例與您如何更改它之間的區別是流量控制的問題。
在原始示例中,只有在數據成功刷新到網絡緩沖區后才會讀取入站通道。 這保證了它只能以盡可能快的速度讀取數據,從而防止Netty的發送隊列堆積,從而防止出現內存不足錯誤。
寫入操作排隊后,更改的代碼將立即讀取。 如果出站頻道無法跟上,那么如果您要傳輸大量數據,則可能會看到內存不足錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.