2014-12-19 92 views
0

我试着让我的本地机器IP地址使用java InetAddress。我可以检查我的网络连接中提到的IP地址与程序返回的IP地址不同。如何在java中获得我的机器的IP地址?

机器的网络连接IP地址显示为192.168.8.192,程序返回的IP地址为127.0.1.1。

我不知道我的程序有什么问题。

如果有人能帮助我,我会非常感激。

InetAddress ina = InetAddress.getLocalHost(); 
    System.out.println("LOCAL IP ADDRESS ===== >" +ina.getHostAddress() + "//" + ina.getHostName()); 

谁能告诉我我在这段代码中做了什么错?

回答

1

试试这个;

Inet4Address.getLocalHost().getHostAddress(); 

或这个;

Inet6Address.getLocalHost().getHostAddress(); 
+0

Semith Eker,谢谢你的回答,我也试过了。它返回相同的地址127.0.1.1。 – 2014-12-19 11:34:17

+0

我编辑为Inet6Address,你可以试试吗? – 2014-12-19 11:41:16

+0

没有进展:(同样的结果。 – 2014-12-19 12:33:38

相关问题