java Netty:在 messageReceived 中获取远程 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11865165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Netty: getting remote ip address in messageReceived
提问by northernd
In my class (extends SimpleChannelHandler) I'm trying to get the ip where the message was originally sent from.
在我的班级(扩展 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));
..
This prints ip 10.0.0.1 (gateway), instead of the correct client address (10.52.45.4).
这将打印 ip 10.0.0.1(网关),而不是正确的客户端地址 (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 ?
有什么方法可以获取我想要的 ip 或者网络配置有问题吗?
采纳答案by Norman Maurer
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.
我猜您会看到网关 ip,因为网关执行某种 NAT。如果是这样,您唯一的机会就是在您的协议中包含源 IP 地址并从那里提取它。
回答by Hallowizer
Ip addresses starting with 10.0.0 are internal, you are probably connecting it to something on the same WiFi router. To get the 10.52.45.4 ip, you have to connect outside of your router. (don't forget to port forward)
以 10.0.0 开头的 IP 地址是内部的,您可能将它连接到同一 WiFi 路由器上的某些东西。要获得 10.52.45.4 ip,您必须连接到路由器外部。(不要忘记转发)