简体   繁体   English

localhost无法解析为127.0.0.1

[英]localhost doesn't resolve to 127.0.0.1

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. Inet4Address.getByName("localhost")调用Inet4Address.getByName("localhost")期望看到127.0.0.1 ,但我看到我的IP被分配到我的以太网/机场。 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. 如果我断开以太网/机场的连接,我将得到127.0.0.1的分辨率,但是一旦我再次连接,它就会解析为以太网/机场的IP地址。

I'm using OSX 10.6.7 and java version "1.6.0_24". 我使用的是OSX 10.6.7和java版本“1.6.0_24”。 My /etc/hosts file has "127.0.0.1 localhost" . 我的/ etc / hosts文件有"127.0.0.1 localhost" Ping localhost in the terminal does resolve to 127.0.0.1. 终端中的Ping localhost确实解析为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. 我需要这个的原因是我正在进行GWT开发,我有一个不同的JVM试图调用GWT托管模式,我得到一个ConnectionException:连接被拒绝,如果localhost没有解析为127.0.0.1。

Thanks 谢谢

I had the same issue and it was because my hostname was set to localhost. 我有同样的问题,因为我的主机名设置为localhost。 I changed it and it fixed the issue. 我改变了它,它解决了这个问题。

I've also seen this behavior on MacOS (v10.9) when the hostname is not set. 当主机名未设置时,我也在MacOS(v10.9)上看到了这种行为。 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. 请注意,使用127.0.0.1是一种解决方法,但您可能希望正确排序,因为localhost应该是127.0.0.1,如果不是,其他内容(如数据库连接等)可能会中断。

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 Ping可能工作正常,但是如果你没有在端口上监听网络服务器:80,那么你将无法在浏览器中得到任何响应...周一早上陷入困境,呃XD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM