本地主机无法解析为 127.0.0.1
我调用 Inet4Address.getByName("localhost")
期望看到 127.0.0.1
,但我看到的是分配给我的以太网/机场的 IP。如果我断开以太网/机场的连接,我将获得 127.0.0.1
的解析,但一旦我再次连接,它就会解析为以太网/机场的 IP 地址。
我正在使用 OSX 10.6.7 和 java 版本“1.6.0_24”。我的 /etc/hosts 文件有 “127.0.0.1 localhost”
。在终端中 Ping localhost 确实解析为 127.0.0.1。
我需要这个的原因是我正在进行 GWT 开发,其中我有一个不同的 JVM 尝试调用 GWT 托管模式,并且如果 localhost 未解析为 127.0.0.1,我会收到 ConnectionException: 连接被拒绝。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,这是因为我的主机名设置为 localhost。我改变了它并解决了这个问题。
I had the same issue and it was because my hostname was set to localhost. I changed it and it fixed the issue.
当未设置主机名时,我还在 MacOS (v10.9) 上看到了这种行为。转到系统偏好设置 ->共享并验证计算机名称是否已填写值。
请注意,使用 127.0.0.1 是一种解决方法,但您可能希望正确解决此问题,因为 localhost 应该是 127.0.0.1,如果不是,则需要执行其他操作(如数据库连接等)可能会中断。
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.
与后来的人切线相关:
确认您正在请求正确的端口也可能很有用!
Pinging 可能工作正常,但如果您没有在端口 :80 上侦听的 Web 服务器,那么您将在浏览器中得不到响应...周一早上的陷阱,呃 XD
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