netty版本是:
<!-- netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.63.Final</version>
</dependency>
环境:
宿主机IP:
IPv4 地址 . . . . . . . . . . . . : 192.168.1.185
虚拟机是NAT模式,
虚拟机IP:
ens33:
inet 192.168.110.129
部署在宿主机的服务端,在接收消息的方法中获取客户端IP
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object frame) throws Exception {
InetSocketAddress inetSocketAddress = (InetSocketAddress) channelHandlerContext.channel().remoteAddress();
// 获取到的客户端IP
InetAddress address = inetSocketAddress.getAddress();
}
我希望获取到的IP是给虚拟机分配的IP:192.168.110.129
但却获取到了宿主机的IP:192.168.1.185;问题是获取不到192.168.110.129这个IP。
debug看的信息
这个46518应该是idea的端口。
同时我还试了http请求时获取
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
但获取的还是192.168.1.185,而不是192.168.110.129
这是为什么,该怎么获取虚拟机的IP呢?