4

I make a call to Inet4Address.getByName("localhost") expecting to see 127.0.0.1, but instead I see my IP assigned to my ethernet/airport. If I disconnect my ethernet/airport, I will get the resolution of 127.0.0.1, but as soon as I connect again it resolves to the IP address of the ethernet/airport.

I'm using OSX 10.6.7 and java version "1.6.0_24". My /etc/hosts file has "127.0.0.1 localhost". Ping localhost in the terminal does resolve to 127.0.0.1.

The reason I need this is that I'm doing GWT development where I have a different JVM trying to make a call to GWT Hosted Mode, and I get a ConnectionException: connection refused, if localhost doesn't resolve to 127.0.0.1.

Thanks

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
JeffgNpc
  • 314
  • 4
  • 11
  • 2
    If you already know you have to go to 127.0.0.1 then just point to it instead of resolving. It can be platform specific since on my linux box it resolvs 127.0.0.1 – guax May 12 '11 at 22:12
  • I don't own the code that makes the call, but if I can't figure out how to make it work, I will be asking the people to make that change. – JeffgNpc May 12 '11 at 22:52
  • Try to capitalize as in "Localhost" and see if it makes a difference. – Terje Norderhaug May 13 '11 at 19:38
  • Have a look at http://stackoverflow.com/questions/7382602/what-is-the-difference-between-127-0-0-1-and-localhost In short, it seems a name resolution quirk. On my machine (Mac OSX), I tried `ping 127.0.0.1`and `ping` with the IP address of my ethernet port; the timings looked the same. That would suggest (I am not sure, maybe someone else can answer/confirm) that even if it shows an IP address assigned to an ethernet port, it does not send the data through the network. – Sci Prog Sep 22 '16 at 01:59

3 Answers3

1

I had the same issue and it was because my hostname was set to localhost. I changed it and it fixed the issue.

Zip
  • 11
  • 1
0

I've also seen this behavior on MacOS (v10.9) when the hostname is not set. Go to to System Preferences -> Sharing and verify the computer name has a value filled in.

Note that using 127.0.0.1 is a workaround, but you probably want to sort this out properly, since localhost is supposed to be 127.0.0.1, and if it's not, other things (like database connections etc.) might break.

jlevy
  • 2,958
  • 2
  • 16
  • 11
0

Tangentially relevant for those who come after:

It may also be useful to confirm that you're requesting the proper port!

Pinging may work fine, but if you've got no web server listening on port :80, then you'll get no response in the browser... Monday morning gotchas, eh XD

ptim
  • 14,902
  • 10
  • 83
  • 103