当前位置: 首页 > 面试题库 >

InetAddress.getLocalHost()。getHostAddress()返回127.0.1.1 [重复]

常自强
2023-03-14
问题内容

这个问题已经在这里有了答案

java InetAddress.getLocalHost(); 返回127.0.0.1…如何获得REAL
IP?
(11个答案)

7年前关闭。

我的问题类似于这个问题。我想获取机器的真实IP(不是127.0.0.1),但是很奇怪,Ubuntu中的以下代码返回了127.0.1.1。

InetAddress.getLocalHost().getHostAddress()

以下是我的完整代码,最初在此处发布在SO中

public String getMachineIP() {
    try {
        String hostIP = InetAddress.getLocalHost().getHostAddress();
        if (!hostIP.equals("127.0.0.1")) {
            return hostIP;
        }

        /*
         * Above method often returns "127.0.0.1", In this case we need to
         * check all the available network interfaces
         */
        Enumeration<NetworkInterface> nInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (nInterfaces.hasMoreElements()) {
            Enumeration<InetAddress> inetAddresses = nInterfaces
                    .nextElement().getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                String address = inetAddresses.nextElement()
                        .getHostAddress();
                if (!address.equals("127.0.0.1")) {
                    return address;
                }
            }
        }
    } catch (UnknownHostException e1) {
        System.err.println("Error = " + e1.getMessage());
    } catch (SocketException e1) {
        System.err.println("Error = " + e1.getMessage());
    }
    return null;
}

上面的代码返回127.0.1.1,而ifconfig在我的Ubuntu机器上则给出以下输出

root@dell:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:21:70:b7:30:cd  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:28 Base address:0x6000

eth1      Link encap:Ethernet  HWaddr 00:22:68:d3:02:b5  
          inet addr:192.168.2.112  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::222:68ff:fed3:2b5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23827 errors:0 dropped:0 overruns:0 frame:32515
          TX packets:23200 errors:46 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:22027719 (22.0 MB)  TX bytes:3778268 (3.7 MB)
          Interrupt:19

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:402 errors:0 dropped:0 overruns:0 frame:0
          TX packets:402 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:29197 (29.1 KB)  TX bytes:29197 (29.1 KB)

我在主机文件中找到了127.0.1.1条目(对我来说很奇怪,因为我从未更新过此文件)

root@dell:~# cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   dell

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

如何获取我的机器的真实IP(不是127.0.0.1)?我只在寻找IPv4地址,不包括127.0.0.0/8 subnet


问题答案:

您将需要使用NetworkInterface枚举网络接口。InetAddress.getLocalHost()总是返回环回。这并不能解释为什么得到127.0.1.1而不是127.0.0.1的原因,但是由于该方法没有执行您要尝试执行的操作,因此它似乎没有特别的意义。请参阅:http
:
//docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()



 类似资料:
  • 问题内容: 我正在写一个简单的网络应用程序…我需要知道我的机器在网络上的真实IP,例如192.168.1.3。getLocalHost返回127.0.0.1(在Linux上,如果在Windows上相同,则为dunno)怎么做? 问题答案: 由于机器可能有多个地址,因此很难确定哪个地址适合您。通常,您希望系统根据其路由表分配IP。由于结果取决于您要连接的IP,因此有一个简单的技巧:只需创建一个连接并

  • 问题内容: 我有一个build.gradle任务,它像我的dev框上的冠军一样工作,生成了一个属性文件,该文件记录了生成生成的机器的名称。逻辑很简单… 在我的开发箱中,这总是产生与bash shell中的主机名相同的值。 但是,在我们的jenkins CI服务器上,bash主机名返回一件事,但是我对InetAddress.getLocalHost()。getHostName()的调用;返回其他内容

  • 问题内容: 考虑到此代码,我是否可以 绝对确定 该块始终执行,无论它是什么? 问题答案: 是的,将在执行或代码块后调用。 唯一不会被调用的时间是: 如果您调用 如果您调用 如果JVM首先崩溃 如果JVM在或块中达到了无限循环(或其他不间断,不终止的语句) 操作系统是否强行终止了JVM进程;例如,在UNIX上 如果主机系统死机;例如,电源故障,硬件错误,操作系统崩溃等 如果该块将由守护程序线程执行并

  • 我试图将三列作为一个字符串,但由于某些原因,我只得到NULL。 以下是代码: places表包含name、city和country三列,它不是空的,但在某些列中可以包含NULL。 结果是: 我试着从这篇帖子中得到答案,如果任何字段包含NULL,MySQL CONCAT将返回NULL,但它对我不起作用。

  • 我在ViewDidLoad函数中有一些代码,它将在调用堆栈的末尾设置一个类变量。我试图重构代码,使其成为一个单独的函数,它将返回值,而不是设置类变量。 由于我缺乏swift知识,我不确定哪里出了问题,我的函数似乎返回得太厄尔了,因为我可以在调试器中告诉我,它在被设置为之前跳转到return。 我还可以在调试器中看到,内部函数在返回主函数后调用。 如何等待内部调用完成后再返回?或者什么是正确的快速方