简体   繁体   English

主机在专用网络上扫描

[英]Host scanning on a private network

I'm trying to list down all available devices on a local WiFi network. 我正在尝试列出本地WiFi网络上的所有可用设备。 To do this I'm using simple ICMP echo technique to check if a device is connected or not. 为此,我使用简单的ICMP回显技术来检查设备是否已连接。 The problem is the time it takes to scan the entire subnet. 问题是扫描整个子网需要花费时间。 I'm using 7 ms wait time between subsequent scan. 我在后续扫描之间使用了7毫秒的等待时间。 My question is, can I use Java multi threading for this purpose where each thread would work independently to scan different segments? 我的问题是,我是否可以为此目的使用Java多线程,即每个线程将独立工作以扫描不同的段? Would there be any link-layer constraints? 会有链路层约束吗? Thanks for your help! 谢谢你的帮助!

I wrote something a while ago that would scan a local network for all available devices and then scan each port to see if its open as well. 不久前,我写了一些东西,它将扫描局域网中的所有可用设备,然后扫描每个端口以查看其是否也打开。 That is available here . 在这里可用。

Since it's a bit long, ill explain how I did it. 由于时间太长,请病假解释我是怎么做到的。

First create a class like so: 首先像这样创建一个类:

public class AddressWorker implements Runnable
{
    private String address;

    public AddressWorker(String address, Vector<String> validAddresses) { this.address = address; }

    public void run()
    {
        //Your existing code that uses ICMP to listen for a device
        //if the address responds add it to valid addresses
    }
}

Since runnables function similarly to threads we will use them as such. 由于runnable的功能类似于线程,因此我们将照此使用它们。

Vector<String> allRespondedAddress = new Vector(256, 256);
ExecutorService addressExecutor = Executors.newCachedThreadPool();
while (list all ip addresses)
{
    addressExecutor.execute(new AddressWorker(nextAddress, allRespondedAddresses));
}

addressExecutor.awaitTermination(some time);

A CachedThreadPool will run every runnable added to it as a thread. CachedThreadPool将作为线程运行添加到它的每个可运行对象。 It will automatically allocate and optimize resources (eg if one thread is waiting and another behind it needs to do work it will move the workable thread ahead while the other(s) wait). 它将自动分配和优化资源(例如,如果一个线程在等待,而另一个线程在等待工作,它将在其他线程等待时将可工作线程向前移动)。 On my cpu it scans all 256 addresses on the subnet and all 65536 ports per address in about 30 seconds. 在我的CPU上,它将在大约30秒内扫描子网上的所有256个地址以及每个地址的所有65536个端口。 I felt my full code was a bit too long to post here, but its all very accessible and commented on the link i posted. 我觉得我的完整代码太久了,无法在此处发布,但是所有内容都非常易于访问,并评论了我发布的链接。 I'm not a networker so I'm afraid I can't help you with link-layer constraints as I do not know what they are. 我不是网络专家,所以我担心我在链接层约束方面无法提供帮助,因为我不知道它们是什么。

I hope this gets you looking in the right direction. 我希望这能使您朝正确的方向看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM