Java获取我的IP地址 [英] Java getting my IP address

查看:77
本文介绍了Java获取我的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java获取我的Internet IP地址,但是当我的IP地址为192.168.0.xxx

I am trying to get my Internet IP address in Java but I keep getting my local address (ie: 127.0.0.1), when my IP address is 192.168.0.xxx

我正在使用这一行:

InetAddress.getLocalHost().getHostAddress();

这似乎是获取IP地址的标准,但它不是我想要的。每个教程都说使用这一行,所以我有点困惑。

which seems standard to get the IP address, but it is not what I am looking for. Every tutorial says to use this line, so I am a little confused.

有谁能告诉我如何获得正确的IP地址?

Could anyone please let me know how I can get my correct IP address please?

我正在连接到WiFi的设备上运行,我没有使用任何电缆。我使用ifconfig inet addr给出的IP连接到服务器,我希望得到设备的inet addr。我可以检查服务器端套接字的IP,但是如果设备(客户端)告诉服务器他希望其他设备连接哪个IP,那就更好了。

I'm running on a device that is connected to WiFi, and I'm not using any cable. I am connecting to a server using the IP given by ifconfig inet addr, and I am looking to get the device's inet addr. I could check the IP of the socket on the server side, but thought it'd be nicer if the device (client) tells the server which IP he is expecting other devices to connect on.

推荐答案

NetworkInterface 类包含所有相关方法,但请注意,没有我的IP这样的东西。一台机器可以有多个接口,每个接口可以有多个IP。

The NetworkInterface class contains all the relevant methods, but be aware that there's no such thing as "my IP". A machine can have multiple interfaces and each interface can have multiple IPs.

你可以用这个类列出它们,但你从列表中选择哪个接口和IP取决于什么你需要使用这个IP。

You can list them all with this class but which interface and IP you choose from the list depends on what you exactly need to use this IP for.

InetAddress.getLocalHost()不咨询你的接口,它只返回常量127.0.0.1(对于IPv4))

(InetAddress.getLocalHost() doesn't consult your interfaces, it simply returns constant 127.0.0.1 (for IPv4))

这篇关于Java获取我的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆