A small pit encountered by the netty server

 In order to process the previous project quickly, after the server pushes the message to the client, to ensure that the client receives the message, the netty's own listener method is used. code show as below:

ctx.channel()
   .writeAndFlush(...)
   .addListener((ChannelFuture writeFuture) -> {
       //消息发送成功
       if (writeFuture.isSuccess()) {
          //...
       } 
       //消息发送失败
       else {
          ChannelUtils.closeOnFlush(ctx.channel());
       }
   });

  When the push was too strong, I suddenly found that the client would lose packets. I always felt that the tcp protocol ensures reliable transmission, but later I found that it was not the case. Discovery : TCP does not guarantee that your mail has been received. It provides a reliable stream of ordered bytes. It knows nothing about your message, nor can it tell you when the remote host received the message. So Netty will not. Netty can only tell you that your message has been written to the operating system buffer before transmission. TCP only guarantees that the process is not lost, it does not mean that the client will receive it. This TCP is unaware.

  Suddenly wake up, can't be lazy, Ma Dan must wait for the client to give a receipt after receiving the message, this needs to be processed at the application layer, and immediately alert. Haha

Guess you like

Origin blog.csdn.net/NICVSY/article/details/112223000