Why does InetAddress.getLocalHost work differently in Win and Linux?

Hi,When I call InetAddress.getLocalHost() in Linux, it always returns 127.0.0.1.When calling in Windows, it returns the IP of the 1st (or unique) network card.I really need to get the network card IP in both systems.
[244 byte] By [Edilmar_Alvesa] at [2007-11-26 17:12:05]
# 1

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.

command0a at 2007-7-8 23:39:58 > top of Java-index,Core,Core APIs...
# 2
Check your '/etc/hosts' file. In Linux distributions it is commonly misconfigured. Put your real hostname/ip address entry first, then the one for 127.0.0.1/localhost.
ejpa at 2007-7-8 23:39:58 > top of Java-index,Core,Core APIs...
# 3

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

duckbilla at 2007-7-8 23:39:58 > top of Java-index,Core,Core APIs...