gpt4 book ai didi

java - 关于使用小程序检测私有(private)IP地址的建议

转载 作者:行者123 更新时间:2023-11-28 23:04:52 25 4
gpt4 key购买 nike

我在检测连接到我构建的 Web 应用程序的客户端私有(private) IP 时遇到了一些麻烦。 看看我的测试结果(在运行 windows 的机器上):1-在一些机器中(来自不同的位置,国家..)小程序给我正确的 ip 但是2-在其他人中我获得了 ip=127.0.0.1 : 我尝试过什么来解决这个问题? A- 例如:我已经停止了 avast 程序保护(网络防护),小程序开始给我正确的私有(private) ip。 B- 在其他机器上,我尝试了“A 点”,但没有成功 C- 我也编辑了 host 文件,但效果不佳

我需要你帮助我了解正在发生的事情?在哪里寻找以解决这个......请不要回答说“你为什么需要私有(private) ip?它可能会改变......”......我知道所有要连接到我的 web 应用程序的机器,所以我可以配置它们。

我的小程序使用的部分源码:

private String PrivateIP(boolean flag)
{
String s1 = "unknown";
String s2 = getDocumentBase().getHost();
int i = 80;
if(getDocumentBase().getPort() != -1)
i = getDocumentBase().getPort();
try
{
String s = (new Socket(s2, i)).getLocalAddress().getHostAddress();
if(!s.equals("255.255.255.255"))
s1 = s;
}
catch(SecurityException _ex)
{
s1 = "FORBIDDEN";
}
catch(Exception _ex)
{
s1 = "ERROR";
}
if(flag)
try
{
s1 = (new Socket(s2, i)).getLocalAddress().getHostName();
}
catch(Exception _ex)
{
Stat = "Cannot Lookup this IP";
}
return s1;
}

我会给你更多信息: 我试过这个http://www.auditmypc.com/digital-footprint.asp为了从其他方法获取 ip 但结果相同,我还运行了 http://www.auditmypc.com/firewall-test.asp并在我无法获得正确 ip 的机器中获得一条消息,如“恭喜你没有任何端口可以打开”xD ...

提前致谢!

最佳答案

首先,如果有多个网络接口(interface),客户端可以有多个IP地址可用。您的方法返回哪个取决于 new Socket() 打开哪个。

现在,您不必打开套接字即可获取客户端的 IP。你可以做的是像这样枚举它们:

String host = InetAddress.getLocalHost().getHostName();

InetAddress[] addressArray = InetAddress.getAllByName(host);

String[] ipArray = new String[addressArray.length];
for (int i = 0; i < addressArray.length; i++) {
InetAddress addr = addressArray[i];
ipArray[i] = addr.getHostAddress();
}

return ipArray;

现在 ipArray 将保存客户端工作站上可用 IP 地址的列表。

关于java - 关于使用小程序检测私有(private)IP地址的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11470667/

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