Try something like this:
-
import java.net.InetAddress;
import java.util.Scanner;
class Net {
public static void main(String[] args) {
Scanner myAddr = new Scanner(System.in);
String hostAddr = myAddr.next();
try {
InetAddress ipAddr = InetAddress.getByName(hostAddr);
System.out.println(ipAddr);
}
catch (java.net.UnknownHostException exc) {
System.out.println("Host not found");
}
}
}
--
The output should prompt for the hostname. Like my local hostname is: UT1DCS107. I type that and hit Enter, and the output is:
ut1dcs107
ut1dcs107/192.168.1.211
Try that, it should work the same on any OS. localhost should be designated for the loopback interface in TCP/IP. If you're seeing otherwise in Windows, your lmhosts file might be misconfigured.
Hope that helps.
Not sure if this is what you're looking for, but I have found this function to work properly under both windows and linux
public boolean isLoopback(String ip)
{
try
{
for (Enumeration<NetworkInterface> i = NetworkInterface.getNetworkInterfaces(); i.hasMoreElements();)
for (Enumeration<InetAddress> j = i.nextElement().getInetAddresses(); j.hasMoreElements();)
if(ip.equals(j.nextElement().getHostAddress())) return true;
}
catch (SocketException se) {}
return false;
}
Cheers