0

I have find two examples on the web to get the ip address the router has given to my pc. Here is the code:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class tryNet {

public static void displayStuff(String whichHost, InetAddress inetAddr) {
    System.out.println("---------------------");
    System.out.println("host: " + whichHost);
    System.out.println("Canonical host name: " + inetAddr.getCanonicalHostName());
    System.out.println("Host Name: " + inetAddr.getHostName());
    System.out.println("Host Address: " + inetAddr.getHostAddress());
    System.out.println("---------------------");
}


public static void main(String argv[]) {
    try {
        InetAddress inetAddr = InetAddress.getLocalHost();
        displayStuff("localhost", inetAddr);
    }

    catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

}

I have read that after having initialized InetAddress inetAddr = InetAddress.getLocalHost(); I can use the method inetAddr.getHostAddress() to get my ip address, the one given by my router (such as write ifconfig in the terminal in ubuntu, or ipconfig in windows) Instead it returns me my loopback address...(127.0.0.1) Why?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned....maybe its this security manager giving me 127.0.0.1...is it possible to avoid it? – SagittariusA Feb 09 '13 at 17:09
  • You cannot rely on `InetAddress.getLocalHost()` except if you know for sure that the host on which your code will run will never be multi-homed (i.e. have multiple IP addresses). In general `InetAddress.getLocalHost()` should be avoided in production code. – peterh Oct 03 '13 at 07:22

2 Answers2

4

Your PC has multiple interfaces (at least two) and multiple IP addresses (If it's plugged into a network, of course). Typically localhost is going to resolve to 127.0.0.1 (on the loopback interface) and the various methods you are using are going to return that.

The following will show you all the interfaces on the machine and the IP addresses assigned to them:

public static void main(String[] args) throws InterruptedException, IOException
{
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements())
    {
        NetworkInterface n = e.nextElement();
        System.out.println(n.getName());
        Enumeration<InetAddress> ee = n.getInetAddresses();
        while (ee.hasMoreElements())
        {
            InetAddress i = ee.nextElement();
            System.out.println(i.getHostAddress());
        }
    }
}
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
1

Typically you host has a name which points to loopback interface. A DHCP server assigned an IP address. Depending on your dhcp client configuration host may take a new name as well.

kofemann
  • 4,217
  • 1
  • 34
  • 39