在我的類(擴展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