简体   繁体   English

如何以编程方式使用android在网络上找到IP Cam的IP地址

[英]How can I find the IP address of IP Cam on network using android programatically

I need the IP address and port no of IP cam which connected on router. 我需要连接在路由器上的IP cam的IP地址和端口号。 You already know most of the IP Cam have admin,password and Device ID. 您已经知道大多数IP Cam具有admin,密码和设备ID。

I know the Device ID, admin and password, I am not much familiar with java/android. 我知道设备ID,管理员和密码,对Java / android不太熟悉。 If anyone share me the tutorial/blog or code snippet of java and xml with explicitly add permissions 如果有人与我分享具有显式添加权限的Java和xml的教程/博客或代码段

NOTE: Assume android device and IP Cam connected on same router (local network) 注意:假设android设备和IP Cam连接在同一路由器(本地网络)上

NOTE: I have Chinese manufacture IP Cam. 注意:我有中国制造的IP Cam。 Here is the link of product. 这是产品的链接。 Product link 产品链接

I know many App available for this purpose but my goal is to learn and doing something else. 我知道有很多为此目的可用的应用程序,但我的目标是学习和做其他事情。

You could ping all possible adresses to list all adresses that respond and then get their hostname to match it with your device ID.' 您可以ping所有可能的地址,以列出所有响应的地址,然后获取其主机名以使其与您的设备ID相匹配。”

Code example : 代码示例:


import java.util.List;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.concurrent.CyclicBarrier;

public class HostFinder {
    private static int ADDR_MAX = 256;

    private List<InetAddress> reachableAdresses; 
    private String hostname;
    private CyclicBarrier cb;

    public HostFinder(String hostname){
        this.reachableAdresses = Collection.synchronizedArrayList();
    }

    public void find(Handler handler, String hostname){

        // Building possible adresses
        InetAddress localhost = InetAdress.getByName("localhost");
        String[] splitAddr = localhost.toString().split(".");
        String root = splitAddr[0] + "." + splitAddr[1] + "." + splitAddr[2];

        // Callback action when the treahd finishes
        this.cb = new CyclicBarrier(ADDR_MAX, () -> {
            for(InetAddress addr: reachableAdresses){
                if(addr.getHostName().equals(hostname)){
                    handler.handle(add);
                    break;
                }
            }
        });

        // Launching a thread that will ping each address possible on the local network
        for(int i = 0; i < ADDR_MAX; ++i) {
            Thread t = new Thread( () -> {
                InetAddress addr = InetAddress.getByName(root + "." + i);
                if(addr.isReachable(5000)){
                    reachableAdresses.add(addr);
                }
                else{
                    try {
                        this.cbr.await();
                    } catch (Exception ex) {
                        System.out.println("Thread error");
                    } 
                }
            });
            t.start();
        }
    }

    // Async handler to fetch the data
    public interface Handler{
        public void handle(InetAddress addr);
    }
}

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

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