简体   繁体   English

TCP Netty确保收到消息

[英]TCP Netty Ensure message received

I am creating client-server system, that should be able to work in ustable network. 我正在创建客户端 - 服务器系统,应该能够在不稳定的网络中工作。 It assumes that connection could be broken at one time, and then system must reconnect and continue it's work. 它假定连接可能一次断开,然后系统必须重新连接并继续工作。 I'am using Netty and stucked with a problem: how do we know that message, which we sent, was received by another host? 我正在使用Netty并遇到一个问题:我们如何知道我们发送的消息是否被其他主机接收?

I was thinking, for this purpose ChannelFuture could be used, and i can simply try to send message again if it fails in attached future listener: 我在想,为此可以使用ChannelFuture,如果附加的未来监听器失败,我可以简单地尝试再次发送消息:

ChannelFuture fut = channel.write(message);
fut.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                LOGGER.error("Message send failed. Message: " + message, future.getCause());
                //Queue message to be send after reconnect
            } 
        }
});

But when i done, i noticed, that listener never prints error. 但是,当我完成操作时,我注意到,该侦听器永远不会输出错误。 (I tested this by unpluging from network when system hardly works) Also i noticed that all futures for messages, which i send, comes into 'done' state and there is way to no ensure message was received (not using acknowledge messages) (当我的系统几乎无法正常工作时,我通过从网络上拔下来测试了这一点)我也注意到我发送的消息的所有未来都进入“完成”状态,并且无法确保收到消息(不使用确认消息)

As i know, TCP protocol guaranties that message would be received, and when working with it we can know which send packages reached their destination and which not. 据我所知,TCP协议保证会收到消息,在使用它时我们可以知道哪些发送包到达目的地,哪些不到。 I can't believe that Netty does not allow to know it. 我无法相信Netty不允许知道它。

Is there a good way to know that message was delivered? 有没有一种好方法可以知道消息已经发送?

You're misunderstanding TCP. 您误解了TCP。 TCP does not guarantee that your message is received. TCP不保证您的邮件已收到。 It provides a reliable, ordered byte stream. 它提供可靠的有序字节流。 It doesn't know anything about your message and cannot tell you when it has been received by the remote host. 它对您的消息一无所知,也无法告诉您远程主机何时接收到该消息。 Therefore neither can Netty. 因此,Netty也不能。 Netty can only tell you that your message has been written to the operating system buffers before it is transmitted. Netty只能告诉您消息在传输之前已被写入操作系统缓冲区。 This is what your code is detecting. 这是您的代码正在检测的内容。

To be sure that your message has been received, the receiver must send back an explicit acknowledgement. 为确保收到您的消息,接收方必须发回明确的确认。 The best way to achieve this depends on the expected behaviour of your protocol but usually involves maintaining a table of all messages that have been sent but not yet acknowledged. 实现此目标的最佳方法取决于协议的预期行为,但通常涉及维护已发送但尚未确认的所有消息的表。

All your future does is write the message to the network. 您的所有未来都是将信息写入网络。

To have guaranteed reception you must either implement an acknowledgment or use message numbers and resend requests. 要保证接收,您必须实现确认或使用消息号并重新发送请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM