Netty和TCP-如何正确发送空消息 [英] Netty and TCP - How to properly send an empty message

查看:622
本文介绍了Netty和TCP-如何正确发送空消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Netty编写的AWS Network ELB(类似于具有长期连接的Echo服务器)后面有一个简单的TCP服务器,我正在尝试实现与TCP keep-alive机制相似的keep-alive机制.空闲连接打开.不幸的是,我们不能依靠TCP保持活动机制,因为NELB不会将保持活动的TCP数据包转发到负载均衡器的另一端.
我想做的是监视空闲连接,并将空字符串(空字节数组)发送给客户端.到目前为止,我在代码中所做的是:

We have a simple TCP server behind an AWS Network ELB (similar to Echo server with long-lived connections) written in Netty and I'm trying to implement a keep-alive mechanism similar to TCP keep-alive mechanism to keep our idle connections open. Unfortunately we cannot rely on TCP keep-alive mechanism since NELBs do not forward keep-alive TCP packets to the other side of the loadbalancer.
What I'm thinking to do is to watch for idle connections and send an empty string (empty byte array) to clients. What I did so far in the code is:

  1. 添加具有某些超时值的IdleStateHandler
  2. 注册一个GprsKeepAliveHandler,它是ChannelDuplexHandler的子类,并覆盖发送Unpooled.EMPTY_BUFFERuserEventTriggered方法.
  1. Add a IdleStateHandler with some timeout values
  2. Register a GprsKeepAliveHandler, a sub class of ChannelDuplexHandler, overriding userEventTriggered method sending (ctx.writeAndFlush) the Unpooled.EMPTY_BUFFER.

这样,如果连接断开,我希望收到一个RST数据包.否则,连接将再次变为活动状态.

This way, I expect to receive a RST packet if the connection is gone. Otherwise the connection will become active again.

问题是Netty对空消息不执行任何操作,它不向客户端发送任何数据包(由Wireshark监视).如果将消息更改为Unpooled.wrappedBuffer(new byte[]{0}),我会看到期望的结果.

The problem is Netty does not do anything with the empty message, it does not send any packets to the client (monitored with Wireshark). If I change the message to Unpooled.wrappedBuffer(new byte[]{0}) I see what I'm expect to see.

问题

  1. 我找不到实现目标的更好方法(使连接保持活动状态并检测无效连接).如果有更好的方法,请告诉我.
  2. 在Netty中发送空消息的正确方法是什么? (我看到了这个问题,但没有帮助)
  3. 如果该问题是由于OS TCP堆栈行为引起的,是否有解决此问题的方法?
  1. I couldn't find a better way to achieve my objective (keep connections alive and detect dead connections). If there's a better way please let me know.
  2. What is the proper way to send an empty message in Netty? (I saw this question but it didn't help)
  3. If the issue is because of OS TCP stack behavior, is there a way to solve this problem?

推荐答案

在出现空消息的情况下,Netty似乎不会进行任何系统调用. (请参见)

It seems that Netty does not make any syscall in case of empty messages. (see this)

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

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