我在我的Win7 PC上設置了一個回送適配器,以便我可以使用Netty綁定它。事情是我分配了Loopback地址的IP地址172.16.1.1 /24
(這是必需的)。但是當我嘗試通過Netty進行綁定時,它會綁定到127.0.0.1
地址 - 稱爲默認本地主機IP。所以我只是想知道如何解決這個問題?我需要它特別綁定到IP地址爲172.16.1.1
和端口162
的環回適配器。Netty綁定到錯誤的接口
相關圖片:
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();
}
}
}
問題是,127.0.0.1 *是*環回地址,而172.16.1.1不是。 – EJP