简体   繁体   中英

How to know ip address with java.net InetAddress or other API

I have UDP server to listen messages, and I need to bind to device IP address. Other devices send messages on the UDP server. I use 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)

I run my code on different environments and see next: On my local machine with win10.getByName("localhost") works and.getLocalHost() not worked. Also other devices (emulators) send messages on "localhost".

On other remote PC with Win7 and some IP I'm using (1) and it works. Physical devices send messages on server IP and it process them. Also, I'm using bridge to allow devices communicate with each other, ie devices placed in different networks (I don't understand this configuration, it is not my).

On this remote PC but in Linux I can set address only manually, ie variant (3). But I need specify it automatically.

I can't get the correct server address by any method. How I can get device address? Maybe there are some another methods or API?

UPD: I'm using netty udp server with standard configuration:

@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) {......

A host may have several network interfaces connected to different networks, and the bind address tells the system which interface you want to listen on. This is typically configured by the user of your application (system administrator) because the networks have different purposes (for example, data plane vs control plan: one network used by system and network admins to control the machine, another network used for production traffic)

If you don't know which interface you should listen on, you can listen on all local interfaces. You do that by binding to the special 0.0.0.0 or:: IP address.

One way you can create the 0.0.0.0 address by first creating a SocketAddress with the InetSocketAddress(int port) constructor, and then retrieving the address from it:

InetAddress anyAddress = new InetSocketAddress(LOG_PORT).getAddress();

Another way is creating the the address directly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM