25

Im trying to get the local IP. It should work with

System.out.println(Inet4Address.getLocalHost().getHostAddress());

or

InetAddress addr = InetAddress.getLocalHost();
ip = addr.getHostAddress();
System.out.println("Ip: " + ip);

but it always returns 192.168.178.154 instead of 192.168.178.119(This is my real local IP(Terminal --> ifconfig))

What should I do?

1
  • 1
    Have you Lan and WiFi? Commented Oct 20, 2013 at 11:36

2 Answers 2

37

Sounds like you have two IP addresses.

On a computer that has one network adapter, the IP address that is chosen is the Primary IP address of the network adaptor in the computer. However, on a multiple-homed computer, the stack must first make a choice. The stack cannot make an intelligent choice until it knows the target IP address for the connection.

When the program sends a connect() call to a target IP address, or sends a send() call to a UDP datagram, the stack references the target IP address, and then examines the IP route table so that it can choose the best network adapter over which to send the packet. After this network adapter has been chosen, the stack reads the Primary IP address associated with that network adapter and uses that IP address as the source IP address for the outbound packets.

Document

If you want to activate second IP and its for example LAN, unplug it and after 10 sec plug in back. Other IP might be selected as host IP in routing table.

You can get 2nd IP from getNetworkInterfaces.

Try to run followed code:

public static void main(String[] args) throws Exception
{
    System.out.println("Your Host addr: " + InetAddress.getLocalHost().getHostAddress());  // often returns "127.0.0.1"
    Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
    for (; n.hasMoreElements();)
    {
        NetworkInterface e = n.nextElement();

        Enumeration<InetAddress> a = e.getInetAddresses();
        for (; a.hasMoreElements();)
        {
            InetAddress addr = a.nextElement();
            System.out.println("  " + addr.getHostAddress());
        }
    }
} 
Sign up to request clarification or add additional context in comments.

3 Comments

If you want to activate second and its LAN, unplug it and after 10 sec plug in back. It will switch to second address as host
Well, you have routing table and PC takes "oldest" interface witch has internet.
no, its not code problem just routing table of your PC
9

if your system is configured with multiple ip then do like this.

try {
  InetAddress inet = InetAddress.getLocalHost();
  InetAddress[] ips = InetAddress.getAllByName(inet.getCanonicalHostName());
  if (ips  != null ) {
    for (int i = 0; i < ips.length; i++) {
      System.out.println(ips[i]);
    }
  }
} catch (UnknownHostException e) {

}

2 Comments

Yeah it seems like, this returns 3 Ips...
@Tyratox You got 192.168.178.119 ip in the list?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.