【问题标题】:In java, what's the difference between InetAddress.getLocalHost() and InetAddress.getByName("127.0.0.1")在java中,InetAddress.getLocalHost() 和 InetAddress.getByName("127.0.0.1") 有什么区别
【发布时间】:2013-12-18 08:33:03
【问题描述】:

我正在使用 Windows 8JDK 1.7。我的IP地址是192.168.1.108,运行时:

System.out.println(InetAddress.getLocalHost().equals(InetAddress.getByName("localhost"))); 

System.out.println(InetAddress.getLocalHost().equals(InetAddress.getByName("127.0.0.1")));

输出 - 都是假的。

InetAddress.getLocalHost() - Output: 192.168.1.108      
InetAddress.getByName("localhost") - Output: 127.0.0.1

此外,我的 UDP 服务器绑定在 InetAddress.getLocalHost() 上,如果客户端将数据包发送到 InetAddress.getByName("localhost"),它就无法从客户端接收任何内容。但是,如果客户端发送到InetAddress.getLocalHost(). 端口是正确的,它会很好地工作。

有人知道区别吗?提前致谢。

【问题讨论】:

  • 可能getLocalHost() 方法会进行名称解析并将地址解析为 127.0.0.1(或任何其他 ip)。于是问题就变成了:localhost和127.0.0.1有什么区别。在这里查看:stackoverflow.com/questions/7382602/…
  • 不,System.out.println(InetAddress.getByName("localhost").equals(InetAddress.getByName("127.0.0.1"))) 给了我真的
  • 因为getByNa‌megetLocalhost 方法都会返回一个InetAddress 对象。 equals 方法在 InetAddress 类中被覆盖,如果 IP 相同,则返回 true。在这里查看:docs.oracle.com/javase/7/docs/api/java/net/…

标签: java networking udp


【解决方案1】:

来自 getLocalHost() 的 JDK 文档:

返回本地主机的地址。这是通过从系统中检索主机的名称,然后将该名称解析为 InetAddress 来实现的。

在我的 GNU/Linux 机器中,我的主机名是“laptop”,它映射到 /etc/hosts 中不同于 127.0.0.1 的地址。在 C:\Windows\System32\drivers\etc\hosts 在 Windows 中有一个等效文件。

默认情况下,在 DNS 查找之前搜索此主机文件。

【讨论】:

    【解决方案2】:

    ad1 为您提供了您在 LAN/WAN 中的地址,看起来 (我的意思是本地/私有网络 IP 地址,例如 192.168.0.108 或类似 10.3.6.55)。

    另见:

    http://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces

    http://download.java.net/jdk7/archive/b123/docs/api/java/net/InetAddress.html#getLocalHost%28%29

    但请注意,在我的示例中,ad2 和 ad3 是相等的。

    import java.net.InetAddress;
    
    public class Test014 {
    
        public static void main(String[] args) throws Exception {
            InetAddress ad1 = InetAddress.getLocalHost();
            InetAddress ad2 = InetAddress.getByName("localhost");
            InetAddress ad3 = InetAddress.getByName("127.0.0.1");
    
            printArr(ad1.getAddress());
            printArr(ad2.getAddress());
            printArr(ad3.getAddress());
    
            System.out.println(ad1.equals(ad2));
            System.out.println(ad1.equals(ad3));
            System.out.println(ad2.equals(ad3));
        }
    
        static void printArr(byte[] arr){
            for (int i=0; i<arr.length; i++){
                System.out.print("[" + i + "] = " + arr[i] + "; ");
            }
            System.out.println();
            System.out.println("---------");
        }
    
    }
    

    此外,请查看 API 文档,了解 equals 方法何时返回 true 以及何时返回 false。

    http://download.java.net/jdk7/archive/b123/docs/api/java/net/InetAddress.html#equals%28java.lang.Object%29

    【讨论】:

    • 现在我明白为什么 equal 返回 false 了。但我只有一个网络适配器。如果我在 192.168.1.108 上绑定 udp 套接字,发送到 127.0.0.1 的数据包也应该收到,对吗?
    • 我认为 - 不是真的。看这里。 stackoverflow.com/questions/7556811/… 我知道服务器通常有特殊的标志/开关来告诉他们监听所有已知的地址/接口。你实际上是在写一个服务器。因此,您为传入连接打开服务器端口的地址很重要。 – peter.petrov 5 分钟前
    猜你喜欢
    • 2019-05-20
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2011-09-28
    • 2020-12-18
    • 1970-01-01
    相关资源
    最近更新 更多