java中的ip地址

发布于 2024-07-30 18:30:29 字数 91 浏览 3 评论 0原文

如果我在大学的话,我会被要求激活一段代码。 所以我需要找到我所在的 iP 来匹配我的大学 iP。 想知道如何在 java 中做到这一点吗? 我已经尝试过环回接口。

I've been asked to activate a certain piece of code if i was in my college. So I need to find the iP of where i am to match to my colleges iP. Was wonderng how to do this in java? I have already tried a loop back interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-08-06 18:30:29

通过使用 NetworkInterface.getNetworkInterfaces() 并在每个接口上调用 getInetAddresses() ,您可以查看分配给您计算机的所有 IP 地址。 要检查您的 IP 是否在您的大学范围内,您可以执行以下操作:

boolean onCampusNetwork() {
    for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
        NetworkInterface iface = ifaces.nextElement();
        for(Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements;) {
            InetAddress address = addresses.nextElement();
            // return true if address is in the university's range; something like:
            if(address.toString().startsWith("10.0")) {
                return true;
            }
        }
    }
    // None of the IP addresses were in the university's range.
    return false;
}

我还没有运行此代码,但它应该可以满足您的需要。

By using NetworkInterface.getNetworkInterfaces() and calling getInetAddresses() on each interface, you can see all IP addresses assigned to your computer. To check if you have an IP in your university's range, you could do something like this:

boolean onCampusNetwork() {
    for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
        NetworkInterface iface = ifaces.nextElement();
        for(Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements;) {
            InetAddress address = addresses.nextElement();
            // return true if address is in the university's range; something like:
            if(address.toString().startsWith("10.0")) {
                return true;
            }
        }
    }
    // None of the IP addresses were in the university's range.
    return false;
}

I haven't run this code, but it should do what you need.

ま昔日黯然 2024-08-06 18:30:29

难道不应该有某种用于自动代理发现和配置的协议吗? 你们的大学已经有这个设置了吗? 如果您的代码发现了正确的设置并具有覆盖这些设置的选项,那就更好了。

Isn't there supposed to be some kind of protocol for automatic proxy discovery and configuration? Does your college have this already setup? Then it would be better if your code discovered the correct settings and had an option to override the settings.

疧_╮線 2024-08-06 18:30:29

有各种各样的网站可以给你你的公共IP(或者你的网关的公共IP,我想这就是你想要的)。 您可以告诉程序与这些站点之一建立 HTTP 连接并获取包含信息的页面。 由于这些网站具有非常可预测的格式,因此使用一两个正则表达式很容易解析结果。 但这仅在您有互联网连接时才有效。

或者,您可以让该程序尝试连接到您大学的一台 Intranet 服务器。 如果它可以连接到外界无法访问的站点,则它位于 LAN 上。

There are all kinds of websites out there that will give you your public ip (or the public IP of your gateway, which I assume is what you want). You could tell the program to make an HTTP connection to one of those sites and get the page with the info on it. Since these sites have a very predictable format, the result would be very easy to parse with a regex or two. This only works if you have an internet connection though.

Alternatively, you could have the program try to connect to one of your college's intranet servers. If it can make the connection to a site that is not accessible to the outside world, it's on the LAN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文