【发布时间】:2011-12-26 09:02:31
【问题描述】:
我正在尝试在 Java 中获取我的 Internet IP 地址,但当我的 IP 地址为 192.168.0.xxx 时,我一直在获取我的本地地址(即:127.0.0.1)
我正在使用这条线:
InetAddress.getLocalHost().getHostAddress();
这似乎是获取 IP 地址的标准方法,但这不是我想要的。每个教程都说要使用这条线,所以我有点困惑。
谁能告诉我如何获取正确的 IP 地址?
我在连接到 WiFi 的设备上运行,但我没有使用任何电缆。我正在使用 ifconfig inet addr 提供的 IP 连接到服务器,并且我正在寻找设备的 inet addr。我可以检查服务器端套接字的 IP,但认为如果设备(客户端)告诉服务器他希望其他设备连接到哪个 IP 会更好。
【问题讨论】:
-
您是否可以通过 localhost 连接到服务器?这段代码是在服务器上运行的,还是在桌面应用程序上运行的,还是什么?
-
什么是“IP地址”?我的电脑目前至少有五个。
-
来自文档的嗯:
If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned. -
我在连接到 WiFi 的设备上运行,但我没有使用任何电缆。我正在使用 ifconfig inet addr 提供的 IP 连接到服务器,并且我正在寻找设备的 inet addr。
-
我可以检查服务器端套接字的 IP,但我认为如果设备(客户端)告诉服务器他希望其他设备连接到哪个 IP 会更好。