在我的类(扩展SimpleChannelHandler)中,我试图获取邮件最初发送来的ip。Netty:在messageReceived中获取远程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));
..
这打印ip 10.0.0.1(网关),而不是正确的客户端地址(10.52.45.4)。
有没有什么办法让ip我试图或可能会出现网络配置错误?
只需在较新的netty 4.1.5'((InetSocketAddress)ctx.channel()。remoteAddress())。getAddress()。getHostAddress() – Aaron 2017-04-03 21:19:29