问题
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
回答1:
I had the same issue and it was because my hostname was set to localhost. I changed it and it fixed the issue.
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/5985034/localhost-doesnt-resolve-to-127-0-0-1