如何使用java.net InetAddress或其他API了解ip地址

如何使用java.net InetAddress或其他API了解ip地址,java,udp,ip-address,inetaddress,Java,Udp,Ip Address,Inetaddress,我有UDP服务器来监听消息,我需要绑定到设备IP地址。其他设备在UDP服务器上发送消息。我使用next: InetAddress address = InetAddress.getLocalHost(); // gives 127.0.1.1 (1) address = InetAddress.getLoopbackAddress(); // 127.0.0.1 (2) address = InetAddress.getByName("123.45.67.8

我有UDP服务器来监听消息,我需要绑定到设备IP地址。其他设备在UDP服务器上发送消息。我使用next:

InetAddress address = InetAddress.getLocalHost();  // gives 127.0.1.1 (1)
address = InetAddress.getLoopbackAddress(); // 127.0.0.1              (2)
address = InetAddress.getByName("123.45.67.89"); // the same          (3)
address = InetAddress.getByName("localhost");  //                     (4)
我在不同的环境中运行代码,请参见下一步: 在我的本地机器上,win10.getByName(“localhost”)工作,而.getLocalHost()不工作。其他设备(模拟器)也会在“本地主机”上发送消息

在另一台装有Win7和我正在使用的IP的远程PC上(1),它可以工作。物理设备在服务器IP上发送消息,并进行处理。此外,我使用网桥允许设备相互通信,即放置在不同网络中的设备(我不理解此配置,它不是我的)

在这个远程PC上,但在Linux中,我只能手动设置地址,即变体(3)。但我需要自动指定它

我无法通过任何方法获得正确的服务器地址。如何获取设备地址?也许还有其他一些方法或API

UPD:我正在使用标准配置的netty udp服务器:

@Override
public void run() {
    final NioEventLoopGroup group = new NioEventLoopGroup();
    try {
        log.info("Started listening logs ...");
        final Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    public void initChannel(final NioDatagramChannel nioDatagramChannel) {
                        ChannelPipeline channelPipeline = nioDatagramChannel.pipeline();
                        channelPipeline.addLast(encryptedPacketHandlerChain);
                    }
                });

        // Bind and start to accept incoming connections.
        InetAddress address = InetAddress.getLocalHost();
        System.out.println("InetAddress..getLocalHost() == " + address.getHostAddress());
        address = InetAddress.getLoopbackAddress();
        System.out.println("InetAddress.getLoopbackAddress == " + address.getHostAddress());
        address = InetAddress.getByName(ip);
        System.out.println("InetAddress.getByName " + ip + " == " + address.getHostAddress());

        bootstrap.bind(address, LOG_PORT).sync().channel().closeFuture().await();

    } catch (Exception e) {......
@覆盖
公开募捐{
final NioEventLoopGroup组=新的NioEventLoopGroup();
试一试{
log.info(“已开始侦听日志…”);
最终引导引导=新引导();
bootstrap.group(group.channel)(NioDatagramChannel.class)
.option(ChannelOption.SO_广播,真)
.handler(新的通道初始值设定项(){
@凌驾
公共无效初始化通道(最终NioDatagramChannel NioDatagramChannel){
ChannelPipeline ChannelPipeline=nioDatagramChannel.pipeline();
channelPipeline.addLast(encryptedPacketHandlerChain);
}
});
//绑定并开始接受传入连接。
InetAddress=InetAddress.getLocalHost();
System.out.println(“InetAddress..getLocalHost()=”+address.getHostAddress());
address=InetAddress.getLoopbackAddress();
System.out.println(“InetAddress.getLoopbackAddress==”+address.getHostAddress());
地址=InetAddress.getByName(ip);
System.out.println(“InetAddress.getByName”+ip+“==”+address.getHostAddress());
bootstrap.bind(地址,日志端口).sync().channel().closeFuture().await();
}捕获(例外情况e){。。。。。。

一台主机可能有多个网络接口连接到不同的网络,绑定地址会告诉系统您要监听哪个接口。这通常由应用程序的用户(系统管理员)配置,因为网络有不同的用途(例如,数据平面与控制计划:系统和网络管理员用于控制机器的一个网络,另一个用于生产流量的网络)

如果您不知道应该在哪个接口上侦听,您可以在所有本地接口上侦听。您可以通过绑定到特殊IP地址来执行此操作

一种创建0.0.0.0地址的方法是,首先使用InetSocketAddress(int-port)构造函数创建
SocketAddress
,然后从中检索地址:

InetAddress anyAddress = new InetSocketAddress(LOG_PORT).getAddress();
另一种方法是直接创建地址:

InetAddress anyAddress = InetAddress.getByAddress(new byte[4]);

什么是“工作”和“不工作”意思是?你得到什么输出?你想要什么输出?主机上的网络接口是如何配置的,你能附上一个屏幕截图吗?如果你绑定到0.0.0.0会发生什么?这可能意味着@Joni Works意味着我绑定到ip并获取消息,not Works意味着我绑定到ip且不接收消息,即服务器侦听另一个host(其他设备发送到正确的端口主机,服务器必须绑定到该端口)。