zoukankan      html  css  js  c++  java
  • 【java】ping IP和IP加端口

      1 /**
      2  * ping ip
      3  *
      4  * @param ip
      5  * @return
      6  */
      7 public static boolean pingIp(String ip) {
      8     if (null == ip || 0 == ip.length()) {
      9         return false;
     10     }
     11     try {
     12         InetAddress.getByName(ip);
     13         return true;
     14     } catch (IOException e) {
     15         return false;
     16     }
     17 }
     18 
     19 /**
     20  * ping ip加端口
     21  *
     22  * @param ip
     23  * @param port
     24  * @return
     25  */
     26 public static boolean pingIpAndPort(String ip, String port) {
     27     if (null == ip || 0 == ip.length() || null == port || 0 == port.length() || !isInt(port) || !isRangeInt(port, 1024, 65535)) {
     28         return false;
     29     }
     30     return pingIpAndPort(ip, Integer.parseInt(port));
     31 }
     32 
     33 /**
     34  * ping ip加端口
     35  *
     36  * @param ip
     37  * @param port
     38  * @return
     39  */
     40 public static boolean pingIpAndPort(String ip, int port) {
     41     if (null == ip || 0 == ip.length() || port < 1024 || port > 65535) {
     42         return false;
     43     }
     44     if (!pingIp(ip)) {
     45         return false;
     46     }
     47     Socket s = new Socket();
     48     try {
     49         SocketAddress add = new InetSocketAddress(ip, port);
     50         s.connect(add, 3000);// 超时3秒
     51         return true;
     52     } catch (IOException e) {
     53         return false;
     54     } finally {
     55         try {
     56             s.close();
     57         } catch (Exception e) {
     58         }
     59     }
     60 }
     61 
     62 /**
     63  * 判断是否是整数
     64  *
     65  * @param str
     66  * @return
     67  */
     68 public static boolean isInt(String str) {
     69     if (!isNumeric(str)) {
     70         return false;
     71     }
     72     // 该正则表达式可以匹配所有的数字 包括负数
     73     Pattern pattern = Pattern.compile("[0-9]+");
     74 
     75     Matcher isNum = pattern.matcher(str); // matcher是全匹配
     76     if (!isNum.matches()) {
     77         return false;
     78     }
     79     return true;
     80 }
     81 
     82 /**
     83  * 匹配是否包含数字
     84  *
     85  * @param str
     86  * @return
     87  */
     88 public static boolean isNumeric(String str) {
     89     if (null == str || 0 == str.length()) {
     90         return false;
     91     }
     92     if (str.endsWith(".")) {
     93         return false;
     94     }
     95     // 该正则表达式可以匹配所有的数字 包括负数
     96     Pattern pattern = Pattern.compile("-?[0-9]+\.?[0-9]*");
     97 
     98     Matcher isNum = pattern.matcher(str); // matcher是全匹配
     99     if (!isNum.matches()) {
    100         return false;
    101     }
    102 
    103     return true;
    104 }
    105 
    106 /**
    107  * 是否在范围内
    108  *
    109  * @param str
    110  * @param start
    111  * @param end
    112  * @return
    113  */
    114 public static boolean isRangeInt(String str, int start, int end) {
    115     if (!isInt(str)) {
    116         return false;
    117     }
    118     int i = Integer.parseInt(str);
    119     return i > start && i < end;
    120 }
  • 相关阅读:
    PythonのTkinter基本原理
    使用 Word (VBA) 分割长图到多页
    如何使用 Shebang Line (Python 虚拟环境)
    将常用的 VBScript 脚本放到任务栏 (Pin VBScript to Taskbar)
    关于 VBScript 中的 CreateObject
    Windows Scripting Host (WSH) 是什么?
    Component Object Model (COM) 是什么?
    IOS 打开中文 html 文件,显示乱码的问题
    科技发展时间线(Technology Timeline)
    列置换密码
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/12570687.html
Copyright © 2011-2022 走看看