查找本地网络中的所有IP地址 [英] Find all IP addresses in local network

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

问题描述

我想在我当前使用 Java 代码连接到的本地网络中查找设备的所有 IP 地址.有用的实用程序

根据这个的回答,我按照以下方式构建了代码:

import java.io.IOException;导入 java.net.InetAddress;公共类 IPScanner{public static void checkHosts(String subnet) 抛出 IOException{int 超时 = 100;for (int i = 1; i <255; i++){字符串主机 = 子网 + "."+我;if (InetAddress.getByName(host).isReachable(timeout)){System.out.println(host + " 可达");}}}public static void main(String[] arguments) 抛出 IOException{checkHosts("192.168.178");}}

不幸的是,这不会打印出任何结果,这意味着无法访问任何 IP 地址.为什么?我的本地网络中有一些设备,就像在 Advanced IP Scanner 扫描中看到的一样.

解决方案

尝试增加超时时间.我使用了大约 5000 毫秒,这对我有帮助.如果您不想等待 5000 毫秒 * 254 = 21 分钟,也可以尝试使用并行 ping 地址的代码:

public static void getNetworkIPs() {最终字节[] ip;尝试 {ip = InetAddress.getLocalHost().getAddress();} 捕获(异常 e){返回;//退出方法,否则ip可能没有被初始化"}for(int i=1;i<=254;i++) {最终 int j = i;//i 作为非最终变量不能被内部类引用new Thread(new Runnable() {//并行执行的新线程公共无效运行(){尝试 {ip[3] = (byte)j;InetAddress 地址 = InetAddress.getByAddress(ip);字符串输出 = address.toString().substring(1);如果(地址.isReachable(5000)){System.out.println(output + "在网络上");} 别的 {System.out.println("无法访问:"+输出);}} 捕获(异常 e){e.printStackTrace();}}}).开始();//不要忘记启动线程}}

非常适合我.

I want to find all IP addresses of devices in the local network I'm currently connected to using Java code. The useful utility Advanced IP Scanner is able to find various IP addresses in my subnet of 192.168.178/24:

According to this answer, I built my code the following way:

import java.io.IOException;
import java.net.InetAddress;

public class IPScanner
{
    public static void checkHosts(String subnet) throws IOException
    {
        int timeout = 100;
        for (int i = 1; i < 255; i++)
        {
            String host = subnet + "." + i;
            if (InetAddress.getByName(host).isReachable(timeout))
            {
                System.out.println(host + " is reachable");
            }
        }
    }

    public static void main(String[] arguments) throws IOException
    {
        checkHosts("192.168.178");
    }
}

Unfortunately, this does not print out any results, meaning that no IP addresses are reachable. Why? There are devices in my local network like seen in the Advanced IP Scanner scan.

解决方案

Try to increase the timeout. I used about 5000ms, this helped me. In case you don't want to wait 5000ms * 254 = 21 minutes, try also this code with parallel pinging to the addresses:

public static void getNetworkIPs() {
    final byte[] ip;
    try {
        ip = InetAddress.getLocalHost().getAddress();
    } catch (Exception e) {
        return;     // exit method, otherwise "ip might not have been initialized"
    }

    for(int i=1;i<=254;i++) {
        final int j = i;  // i as non-final variable cannot be referenced from inner class
        new Thread(new Runnable() {   // new thread for parallel execution
            public void run() {
                try {
                    ip[3] = (byte)j;
                    InetAddress address = InetAddress.getByAddress(ip);
                    String output = address.toString().substring(1);
                    if (address.isReachable(5000)) {
                        System.out.println(output + " is on the network");
                    } else {
                        System.out.println("Not Reachable: "+output);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();     // dont forget to start the thread
    }
}

Worked perfectly for me.

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

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