TCP Netty确保收到消息 [英] TCP Netty Ensure message received

查看:1154
本文介绍了TCP Netty确保收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建客户端 - 服务器系统,应该能够在不稳定的网络中工作。它假设连接可以一次断开,然后系统必须重新连接并继续它的工作。我正在使用Netty并且遇到了一个问题:我们怎么知道我们发送的消息是由另一个主机收到的?

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?

我在想,为此目的可以使用ChannelFuture,如果附加的未来监听器失败,我可以简单地尝试再次发送消息:

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 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)

据我所知,TCP协议保证会收到消息,使用它时我们可以知道哪些发送包到达目的地,哪些不到。我无法相信Netty不允许知道它。

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. I can't believe that Netty does not allow to know it.

有没有一种好方法可以知道邮件已经发送?

Is there a good way to know that message was delivered?

推荐答案

<你是误解TCP。 TCP不保证您的邮件已收到。它提供可靠的有序字节流。它对您的消息一无所知,也无法告诉您远程主机何时收到该消息。因此Netty也不会。 Netty只能告诉您,您的消息在传输之前已写入操作系统缓冲区。这是您的代码正在检测的内容。

You're misunderstanding TCP. TCP does not guarantee that your message is received. 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 can only tell you that your message has been written to the operating system buffers before it is transmitted. 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.

这篇关于TCP Netty确保收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆