northernd northernd - 1 year ago 246
Java Question

Netty: getting remote ip address in messageReceived

In my class (extends SimpleChannelHandler) I'm trying to get the ip where the message was originally sent from.

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));


This prints ip (gateway), instead of the correct client address (

Is there any way to get the ip i'm trying to or could there be something wrong with the network configuration ?

Answer Source

I guess you see the gateway ip because the gateway does some kind of NAT. If so, the only chance you have is to include the source-ip address in your protocol and extract it from there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download