Netty绑定到错误的接口 [英] Netty binding to wrong interface
问题描述
我在Win7 PC上设置了一个环回适配器,以便可以用Netty绑定到它.事情是我给回送地址分配了IP地址172.16.1.1 /24
(这是必需的).但是,当我尝试通过Netty进行绑定时,它改为绑定到127.0.0.1
地址-被称为默认localhost IP.所以我只是想知道如何解决这个问题?我特别需要它来绑定IP地址172.16.1.1
和端口162
的回送适配器.
I set up a loopback adapter on my Win7 PC so that I could bind to it with Netty. The thing is I assigned the Loopback address an IP address of 172.16.1.1 /24
(which is required). But when I try to bind through Netty it instead binds to the 127.0.0.1
address - known as the default localhost IP. So I'm just wondering how I can work around this? I need it SPECIFICALLY to bind to the Loopback adapter with IP address 172.16.1.1
and port 162
.
相关图片: http://i.imgur.com/LdGQZh3.png >
Pictures related: http://i.imgur.com/LdGQZh3.png
NetUtil - Loopback interface: Software Loopback Interface 1
NetUtil - Loopback address: /127.0.0.1 (primary) <--- the problem
NetUtil - Loopback address: /0:0:0:0:0:0:0:1
与代码有关:
public class SnmpServer {
private final Logger logger = Logger.getLogger("SnmpServer");
//private final static String SNMP_HOST_IP = "172.16.1.1";
private static final byte[] SNMP_HOST_ADDR = {(byte)172, (byte)16, (byte)1, (byte)1};
private final static int SNMP_TRAP_PORT = 162;
final ServerBootstrap snmpBootstrap;
public SnmpServer() {
snmpBootstrap = new ServerBootstrap();
}
public void bind() throws Exception {
final EventLoopGroup bossGroup = new NioEventLoopGroup();
final EventLoopGroup workerGroup = new NioEventLoopGroup();
final SocketAddress socket =
new InetSocketAddress(InetAddress.getByAddress(SNMP_HOST_ADDR), SNMP_TRAP_PORT);
try {
snmpBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new SnmpChannelHandler());
final ChannelFuture future = snmpBootstrap.bind(socket).sync();
//future.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
推荐答案
解决方法是Netty绑定到位于我的HOSTS文件中的所有地址.我只是导航到该文件,并在其中添加了172.16.1.1 localhost
的地址,然后称为InetAddress.getByName("localhost")
.
The fix was that Netty was binding to all the addresses located in my HOSTS file. I simply navigated to that file and added the address for 172.16.1.1 localhost
in there and then called InetAddress.getByName("localhost")
.
这篇关于Netty绑定到错误的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!