栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA-扫描局域网、自定义网段IP加端口在线设备

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA-扫描局域网、自定义网段IP加端口在线设备

用JAVA去获取局域网中在线设备的需求,比如我的设备IP是192.168.1.3,我需要先判断是否在线这会你就会想到的是PING但是加上端口呢?比如我的设备IP是192.168.1.3:8080呢?根据IP加端口扫描;以下是我整理出来的解决方案:

  
    public static Map getDeviceOnLineStatus(String networkSegment,int port){
        Map map=new linkedHashMap<>();
        Map resultMap=new HashMap();
        if(StringUtils.isEmpty(networkSegment)){
            try{
                networkSegment = InetAddress.getLocalHost().getHostAddress();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        int lastPoint = networkSegment.lastIndexOf('.');
        String ipHead = networkSegment.substring(0, ++lastPoint);
        for (int num = 1; num < 255; num++) {
            String ip = ipHead + String.valueOf(num);
            map.put(ip,port);
        }
        if(!CollectionUtils.isEmpty(map)){
            Iterator> entries = map.entrySet().iterator();
            while(entries.hasNext()){
                Map.Entry entry = entries.next();
                boolean pingIpAndPort = pingIpAndPort(entry.getKey(),entry.getValue());
                System.out.println("IP为:"+entry.getKey()+",连接状态:"+pingIpAndPort);
                resultMap.put(entry.getKey(),pingIpAndPort);
            }
        }
        return resultMap;
    }
    
    
    public static boolean pingDeviceIp(String ip){
        if(StringUtils.isEmpty(ip)){
            return false;
        }
        if (!pingIp(ip)) {
            return false;
        }
        int timeOut = 3000;
        boolean reachable =false;
        try{
            reachable = InetAddress.getByName(ip).isReachable(timeOut);
        }catch (Exception e){
            e.printStackTrace();
        }
        return reachable;
    }

    
    public static boolean pingIp(String ip) {
        if (null == ip || 0 == ip.length()) {
            return false;
        }
        try {
            InetAddress.getByName(ip);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    
    public static boolean pingIpAndPort(String ip, String port) {
        if (null == ip || 0 == ip.length() || null == port || 0 == port.length() || !isInt(port) || !isRangeInt(port, 1024, 65535)) {
            return false;
        }
        return pingIpAndPort(ip, Integer.parseInt(port));
    }

    
    public static boolean isInt(String str) {
        if (!isNumeric(str)) {
            return false;
        }
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("[0-9]+");

        Matcher isNum = pattern.matcher(str); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    
    public static boolean isNumeric(String str) {
        if (null == str || 0 == str.length()) {
            return false;
        }
        if (str.endsWith(".")) {
            return false;
        }
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("-?[0-9]+\.?[0-9]*");

        Matcher isNum = pattern.matcher(str); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    
    public static boolean isRangeInt(String str, int start, int end) {
        if (!isInt(str)) {
            return false;
        }
        int i = Integer.parseInt(str);
        return i > start && i < end;
    }

测试结果:

用JAVA PING IP 网络解决方案如下:

    
    public static boolean pingDeviceIp(String ip){
        if(StringUtils.isEmpty(ip)){
            return false;
        }
        if (!pingIp(ip)) {
            return false;
        }
        int timeOut = 3000;
        boolean reachable =false;
        try{
            reachable = InetAddress.getByName(ip).isReachable(timeOut);
        }catch (Exception e){
            e.printStackTrace();
        }
        return reachable;
    }

演示结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785929.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号