2

I tried InetAddress.getLocalHost().getHostAddress() to get the users internet address. It's giving me 127.0.1.1 but i'm looking for something more like 192.168.1.75. Any idea how to get the address i'm looking for? Thanks - Tyler

EDIT: I have ubuntu. Remember that. I exported my program to a jar and ran it on my moms Windows laptop. It game me the correct address. That is 192.168.1.64. Still not the correct one on mine.

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Tyler Songer
  • 70
  • 1
  • 9
  • if you run this code locally you will get 127.0.1.1 but if you run from remote pc you will get ip as `192.168.1.75` with your current code. but if you really need your external ip then see this post `http://stackoverflow.com/a/2939223/1262764` – Atul Nar Nov 29 '14 at 08:00
  • possible duplicate of [How to get Ip address of current machine using Java](http://stackoverflow.com/questions/9481865/how-to-get-ip-address-of-current-machine-using-java) – Joe Nov 29 '14 at 08:06
  • @AtulNar&Joe that's not a duplicate; you're pointing to a question about how to get the *external facing IP address* (after having passed through NAT routers etc.). The OP hasn't indicated that's what he wants; it seems he just wants the IP number of the local network interface card. – Erwin Bolwidt Nov 29 '14 at 08:10
  • i want my local ip because i have a method that runs through all of the servers on the local address and with the localhost it doesnt return it. you have to use the 192.168.1.75 for me – Tyler Songer Nov 29 '14 at 08:23

2 Answers2

3

This means that you have misconfigured your local resolver in some way. getLocalHost() is supposed to return the real local IP address, and getLoopbackAddress() returns the loopback address, usually 127.0.0.1 (you say you are getting 127.0.1.1; although that's not impossible, I still assume that's a typo?)

There are several situations that you can identify in the Java code for getLocalHost() that will make it return the loopback address instead of the real address:

  1. The local hostname is set to localhost

        String local = impl.getLocalHostName();
        // [...]
        if (local.equals("localhost")) {
            return impl.loopbackAddress();
        }
    
  2. Your code doesn't have permissions to get the local host adddress (it may be an applet or a Java WebStart application without permissions)

    } catch (java.lang.SecurityException e) {
        return impl.loopbackAddress();
    }
    

In other situations, however, it should throw an UnknownHostException.

If your problem is number 1, then you need to change the host name of your machine to something that resolved back to the IP number of your computer.

If your problem is number 2, then you need to make sure that your code gets the appropriate permission, for example by signing the applet or webstart application.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • it wasnt a type. i copied it straight from the console output. ill try your loopbackaddress method – Tyler Songer Nov 29 '14 at 08:18
  • just tested it and the loopback (`InetAddress.getLoopbackAddress().getHostAddress()`) is returning `127.0.0.1` – Tyler Songer Nov 29 '14 at 08:21
  • What is in your `/etc/hostname` file? – Erwin Bolwidt Nov 29 '14 at 10:00
  • `ty-Satellite-C55-A` is my hostname file. also in the hosts file i found this for the first 2 lines `127.0.0.1 localhost` and `127.0.1.1 ty-Satellite-C55-A` – Tyler Songer Nov 30 '14 at 19:13
  • There you have it; you have configured your machine to have `127.0.1.1` as its address. If you want `getLocalHost()` to return a different address, you need to either change your `/etc/hosts` file and point to its real IP address (or if your DNS is set up correctly, you can remove/comment that line and let the DNS handle the resolution of the name) – Erwin Bolwidt Dec 01 '14 at 02:38
  • Edit `/ets/hosts` , just comment the line with 127.0.1.1 – Alex Belous Jan 24 '17 at 21:09
  • Had this same problem with Java. It was doing a DNS lookup on my machine's hostname, in my case `macbook` which resolves to a CDN. This post helped me solve it. – tresf Sep 30 '19 at 15:57
0

Instead of InetAddress.getLocalHost(), you can use code such as InetAddressLocalHostUtil.java to find the first non-loopback but site local address of the local host ...

Having said that, depending on what you would like to achieve, you may be better off simply using InetAddress.getLoopbackAddress() - it depends if you need an interface that you can connect to externally, or just want to have an address e.g. for two processes on your system to communicate.

vorburger
  • 3,439
  • 32
  • 38