Java获取我的IP地址
我试图在Java中获取我的互联网IP地址,但我不断获取我的本地地址(即:127.0.0.1),当我的IP地址是192.168.0.xxx时,
我正在使用以下行:
InetAddress.getLocalHost().getHostAddress();
这似乎是获取IP地址的标准,但这不是我要找的。每个教程都说要使用这条线,所以我有点困惑。
谁能告诉我如何获得正确的 IP 地址?
我正在连接到 WiFi 的设备上运行,并且没有使用任何电缆。我正在使用 ifconfig inet addr 给出的 IP 连接到服务器,并且我希望获取设备的 inet addr。我可以检查服务器端套接字的 IP,但我认为如果设备(客户端)告诉服务器他期望其他设备连接哪个 IP,那就更好了。
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
I am using the line:
InetAddress.getLocalHost().getHostAddress();
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.
Could anyone please let me know how I can get my correct IP address please?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
网络接口
类包含所有相关方法,但请注意,不存在“我的 IP”之类的东西。一台机器可以有多个接口,每个接口可以有多个IP。您可以在此类中列出所有这些,但从列表中选择哪个接口和 IP 取决于您究竟需要使用该 IP 做什么。
(
InetAddress.getLocalHost()
不会咨询您的接口,它只是返回常量 127.0.0.1(对于 IPv4))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.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()
doesn't consult your interfaces, it simply returns constant 127.0.0.1 (for IPv4))让我们询问 AWS
编辑
在您投反对票之前,我很清楚这不是一个 java 解决方案。它是任何编程语言的通用解决方案。其他解决方案对我来说效果不佳。另外,我相信了解您的 IP 的更简单方法是上网。它可以是任何站点,服务器可以返回它在请求中获得的客户端 IP。您可以为其设置自己的端点。
Let's ask AWS
EDIT
Before you downvote, I'm well aware this is not a java solution. Its a general solution for any programming language. The other solutions don't work as well for me. Also I believe the easier way of knowing your IP is to go on the internet. It can be any site, the server can return back your client ip that it got in the request. You can set up your own endpoint for it.
遇到了同样的问题,在此页面上找到了解决方案: http://mrhawy.blogspot.it/2012/05/how-to-get-your-external-ip-address-in.html
在使用此代码时遇到了一些问题从长远来看,一周内有几次服务器不会回复。
新的解决方案:
Had the same problem, found the solution on this page: http://mrhawy.blogspot.it/2012/05/how-to-get-your-external-ip-address-in.html
had some troubles with this code on the long run, several times in a week the server just won't reply.
new solution:
我的解决方案仅返回 1 个 Ip4 地址:
My Solution which only returns 1 Ip4 address:
默认网络接口的另一种选择,只是我 5 分钟前尝试并看到了你的问题:)
Another option for default network interface, just I was trying 5 min ago and saw your question :)
这是我获取IP地址的方法。
请参阅下面的完整工作代码。
Here is my approach to get the IP address.
Please see below for full working code.
你需要这里获取jsoup的jar
在你的java项目中添加jsoup的jar并解释这行代码,你将得到你的ip地址,
You need to get the jar of jsoup here
add the jar of jsoup in your java project and interpret this lines of code and you will get your ip adress,
您可以通过编写简单的代码来获取您的IP地址。
`
You can get your IP address by writing a simple code.
`