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, i.e. 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, i.e. 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) {......