【发布时间】:2012-08-05 14:25:49
【问题描述】:
在我的班级(扩展 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));
..
这将打印 ip 10.0.0.1(网关),而不是正确的客户端地址 (10.52.45.4)。
有什么方法可以获取我正在尝试的 ip,还是网络配置有问题?
【问题讨论】:
-
使用较新的 netty 4.1.5
((InetSocketAddress )ctx.channel().remoteAddress()).getAddress().getHostAddress();对语法进行了轻微更新
标签: java networking ip netty