Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

    ..

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

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

like image 614
northernd Avatar asked Aug 08 '12 13:08

northernd


People also ask

What is netty used for?

Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.


1 Answers

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.

like image 159
Norman Maurer Avatar answered Sep 19 '22 17:09

Norman Maurer