InetAddress getLocalHost () does not return the expected IP address from C: \ WINDOWS \ system32 \ drivers \ etc \ hosts

In the file C: \ WINDOWS \ system32 \ drivers \ etc \ hosts I have only the following line

192.168.0.23 computername.domain.com computername 

When i started

 InetAddress localhost = InetAddress.getLocalHost(); System.out.println("getLocalHost:" + localhost); 

I expect the conclusion to be

 getLocalHost:computername/192.168.0.23 

but he appears like

 getLocalHost:computername/192.168.0.107 

Any ideas on why this is happening? Should the configuration be done in another file (too)?

EDIT

 InetAddress.getByName('computername') 

creates the same IP address as getLocalHost() .

+4
source share
2 answers

getLocalHost() returns the actual IP address of one of your network adapters. If you are running ipconfig on your command line, one of your adapters should return the same address.

If you have multiple adapters and want to use them, you will need to use NetworkInterface.getNetworkInterfaces() , and then pull the InetAddresses list from each interface.

+14
source

Why do entries from the hosts affect the IP address for localhost ?

InetAddress.getByName('computername') should provide you with the IP address you expect.

+3
source

All Articles