本地网络上ping名单设备名单、设备、网络、ping

2023-09-06 03:44:30 作者:草莓味小可耐

我试图创建一个函数,列出本地网络上的所有连接的设备。 我要做的就是ping通的addresspace XXX0任意地址XXX255,但它似乎并没有正常工作。任何人都可以解释或延长我的code不知何故?我得到的电话(10.0.0.17)和默认网关(10.0.0.138)的响应。后者甚至不应该在那里(说实在的,我不知道什么是默认网关,但忽略了这一点)。我从这个计算机中缺少的IP,但。

 公开的ArrayList< InetAddress类> getConnectedDevices(字符串YourPhoneIPAddress){
    ArrayList的< InetAddress类> RET =新的ArrayList< InetAddress类>();

    LoopCurrentIP = 0;

    // ip地址字符串=;
    的String [] myIPArray = YourPhoneIPAddress.split(\\);
    InetAddress类currentPingAddr;

    的for(int i = 0; I< = 255;我++){
        尝试 {

            //构建下一个IP地址
            currentPingAddr = InetAddress.getByName(myIPArray [0] +。+
                    myIPArray [1] +。 +
                    myIPArray [2] +。 +
                    Integer.toString(LoopCurrentIP));

            // 50ms的超时为平
            如果(currentPingAddr.isReachable(50)){
                如果(currentPingAddr.getHostAddress()!= YourPhoneIPAddress){
                    ret.add(currentPingAddr);

                }
            }
        }赶上(UnknownHostException异常前){
        }赶上(IOException异常前){
        }

        LoopCurrentIP ++;
    }
    返回RET;
}
 

解决方案

下面是稍微修改循环应该做的伎俩(或至少是为我工作);

 尝试{
    NetworkInterface的IFACE =的NetworkInterface
            .getByInetAddress(InetAddress.getByName(此时将));

    的for(int i = 0; I< = 255;我++){

        //构建下一个IP地址
        地址字符串此时将=;
        ADDR = addr.substring(0,addr.lastIndexOf()+1'。')+ I;
        InetAddress类pingAddr = InetAddress.getByName(地址);

        // 50ms的超时为平
        如果(pingAddr.isReachable(IFACE,200,50)){
            Log.d(平安,pingAddr.getHostAddress());
        }
    }
}赶上(UnknownHostException异常前){
}赶上(IOException异常前){
}
 
网络ping不通的原因

I'm trying to create a function that lists all connected devices on a local network. What I do is to ping any address from addresspace x.x.x.0 to x.x.x.255, but it doesn't seem to work properly. Could anyone explain or extend my code somehow? I do get a response from the phone (10.0.0.17) and a default gateway (10.0.0.138). The latter shouldn't even be there (matter of fact I don't know what a default gateway is but ignore that). I am missing the IP from this computer though.

public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
    ArrayList<InetAddress> ret = new ArrayList<InetAddress>();

    LoopCurrentIP = 0;

    //        String IPAddress = "";
    String[] myIPArray = YourPhoneIPAddress.split("\\.");
    InetAddress currentPingAddr;

    for (int i = 0; i <= 255; i++) {
        try {

            // build the next IP address
            currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
                    myIPArray[1] + "." +
                    myIPArray[2] + "." +
                    Integer.toString(LoopCurrentIP));

            // 50ms Timeout for the "ping"
            if (currentPingAddr.isReachable(50)) {
                if(currentPingAddr.getHostAddress() != YourPhoneIPAddress){
                    ret.add(currentPingAddr);

                }
            }
        } catch (UnknownHostException ex) {
        } catch (IOException ex) {
        }

        LoopCurrentIP++;
    }
    return ret;
}

解决方案

Here's slightly modified loop which should do the trick (or worked for me at least);

try {
    NetworkInterface iFace = NetworkInterface
            .getByInetAddress(InetAddress.getByName(YourIPAddress));

    for (int i = 0; i <= 255; i++) {

        // build the next IP address
        String addr = YourIPAddress;
        addr = addr.substring(0, addr.lastIndexOf('.') + 1) + i;
        InetAddress pingAddr = InetAddress.getByName(addr);

        // 50ms Timeout for the "ping"
        if (pingAddr.isReachable(iFace, 200, 50)) {
            Log.d("PING", pingAddr.getHostAddress());
        }
    }
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}