Netty:在messageReceived中获取远程IP地址

nor*_*rnd 11 java ip networking netty

在我的类(扩展SimpleChannelHandler)中,我正在尝试获取最初发送消息的ip.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent ev) throws Exception {
    String host = ((InetSocketAddress)ctx.getChannel().getRemoteAddress()).getAddress().getHostAddress();
    int port = ((InetSocketAddress)ctx.getChannel().getRemoteAddress()).getPort();
    LOG.debug(String.format("host:%s port:%d", host, port));

    ..
Run Code Online (Sandbox Code Playgroud)

这将打印ip 10.0.0.1(网关),而不是正确的客户端地址(10.52.45.4).

是否有任何方法可以获取ip我正在尝试或者网络配置是否有问题?

Nor*_*rer 7

我猜你看到了网关ip,因为网关做了某种NAT.如果是这样,您唯一的机会是在协议中包含source-ip地址并从那里提取它.