gpt4 book ai didi

java - 客户端的外部 IP 地址

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:15:00 24 4
gpt4 key购买 nike

听起来很有趣,但我怎样才能从客户端获取外部 IP 地址呢?

我尝试了一些东西,但对我没有用。

首先我尝试过

request.getRemoteAddr()

我得到的结果是:0:0:0:0:0:0:0:1

我试过第二名

InetAddress ip = InetAddress.getLocalHost();
ip.getHostAddress());

我得到的结果是:127.0.0.1

我试过第三名

        URL whatismyip = new URL("http://checkip.dyndns.org:8245/");
BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

String IPStrOld = inIP.readLine(); //IP as a String
String IPStrNewest = IPStrOld.replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "");
String IPStr = IPStrNewest.replace("</body></html>", "");

但我只得到服务器的外网IP

最后一个地方

        URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
String ip = inIP.readLine();

这个是一样的,我只获取服务器的外网IP

那么,这是怎么回事?

最佳答案

如果您的客户使用 NAT (网络地址翻译)它可能没有外部地址。大多数情况下,根据我的经验,情况就是这样。在工作中,我的网络请求通过代理,因此网络服务器只能确定这个地址。在家里,我通过服务器使用 NAT,所以我正在打字的这台笔记本电脑没有外部地址。最接近的是从我的服务器地址“whatismyip”返回的内容,有时我可能会通过它转发到我的笔记本电脑的端口。

关于java - 客户端的外部 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7040181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com