服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中实现ping功能的多种方法详解

Android中实现ping功能的多种方法详解

2022-11-25 14:23恬静释然 Android

这篇文章主要介绍了Android中实现ping功能的多种方法详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

使用java来实现ping功能。 并写入文件。为了使用java来实现ping的功能,有人推荐使用java的 Runtime.exec()方法来直接调用系统的Ping命令,也有人完成了纯Java实现Ping的程序,使用的是Java的NIO包(native io, 高效IO包)。但是设备检测只是想测试一个远程主机是否可用。所以,可以使用以下三种方式来实现:

1. Jdk1.5的InetAddresss方式

自从Java 1.5,java.net包中就实现了ICMP ping的功能。

使用时应注意,如果远程服务器设置了防火墙或相关的配制,可能会影响到结果。另外,由于发送ICMP请求需要程序对系统有一定的权限,当这个权限无法满足时, isReachable方法将试着连接远程主机的TCP端口 7(Echo)。代码如下:

?
1
2
3
4
5
public static boolean ping(String ipAddress) throws Exception {
   int timeOut = 3000; // 超时应该在3钞以上
   boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
   return status;
 }

2. 最简单的办法,直接调用CMD

?
1
2
3
4
5
6
7
8
9
10
11
12
public static void ping1(String ipAddress) throws Exception {
    String line = null;
    try {
      Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
      BufferedReader buf = new BufferedReader(new InputStreamReader(
          pro.getInputStream()));
      while ((line = buf.readLine()) != null)
        System.out.println(line);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }

3.Java调用控制台执行ping命令

具体的思路是这样的:
通过程序调用类似“ping 127.0.0.1 -n 10 -w 4”的命令,这命令会执行ping十次,如果通顺则会输出类似“来自127.0.0.1的回复: 字节=32 时间<1ms TTL=64”的文本(具体数字根据实际情况会有变化),其中中文是根据环境本地化的,有些机器上的中文部分是英文,但不论是中英文环境, 后面的“<1ms TTL=62”字样总是固定的,它表明一次ping的结果是能通的。如果这个字样出现的次数等于10次即测试的次数,则说明127.0.0.1是百分之百能连通的。
技术上:具体调用dos命令用Runtime.getRuntime().exec实现,查看字符串是否符合格式用正则表达式实现。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public static boolean ping2(String ipAddress, int pingTimes, int timeOut) {
    BufferedReader in = null;
    Runtime r = Runtime.getRuntime(); // 将要执行的ping命令,此命令是windows格式的命令
    String pingCommand = "ping " + ipAddress + " -n " + pingTimes  + " -w " + timeOut;
    try // 执行命令并获取输出
      System.out.println(pingCommand); 
      Process p = r.exec(pingCommand); 
      if (p == null) { 
        return false
      }
      in = new BufferedReader(new InputStreamReader(p.getInputStream()));  // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
      int connectedCount = 0
      String line = null
      while ((line = in.readLine()) != null) { 
        connectedCount += getCheckResult(line); 
      // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
      return connectedCount == pingTimes;
    } catch (Exception ex) { 
      ex.printStackTrace();  // 出现异常则返回假
      return false;
    } finally
      try
        in.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      }
    }
  }
  //若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
  private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+line);
    Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",  Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
      return 1;
    }
    return 0;
  }

4. 实现程序一开始就ping,运行完之后接受ping,并写入文件

完整代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ping {
  private static final String TAG = "Ping";
  private static Runtime runtime;
  private static Process process;
  private static File pingFile;
 /**
   * Jdk1.5的InetAddresss,代码简单
   * @param ipAddress
   * @throws Exception
   */
  public static boolean ping(String ipAddress) throws Exception {
    int timeOut = 3000; // 超时应该在3钞以上
    boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
    return status;
  }
  /**
   * 使用java调用cmd命令,这种方式最简单,可以把ping的过程显示在本地。ping出相应的格式
   * @param url
   * @throws Exception
   */
  public static void ping1(String url) throws Exception {
    String line = null;
    // 获取主机名
    URL transUrl = null;
    String filePathName = "/sdcard/" + "/ping";
     File commonFilePath = new File(filePathName);
    if (!commonFilePath.exists()) {
      commonFilePath.mkdirs();
      Log.w(TAG, "create path: " + commonFilePath);
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    String date = df.format(new Date());
    String file = "result" + date + ".txt";
    pingFile = new File(commonFilePath,file);
    try {
      transUrl = new URL(url);
      String hostName = transUrl.getHost();
      Log.e(TAG, "hostName: " + hostName);
      runtime = Runtime.getRuntime();
      process = runtime.exec("ping " + hostName);
      BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
      int k = 0;
      while ((line = buf.readLine()) != null) {
        if (line.length() > 0 && line.indexOf("time=") > 0) {
          String context = line.substring(line.indexOf("time="));
          int index = context.indexOf("time=");
          String str = context.substring(index + 5, index + 9);
          Log.e(TAG, "time=: " + str);
          String result =
              new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()) + ", " + hostName + ", " + str + "\r\n";
          Log.e(TAG, "result: " + result);
          write(pingFile, result);
        }
      }
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }
 /**
   * 使用java调用控制台的ping命令,这个比较可靠,还通用,使用起来方便:传入个ip,设置ping的次数和超时,就可以根据返回值来判断是否ping通。
   */
  public static boolean ping2(String ipAddress, int pingTimes, int timeOut) {
    BufferedReader in = null;
    // 将要执行的ping命令,此命令是windows格式的命令
    Runtime r = Runtime.getRuntime();
    String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
    try {
      // 执行命令并获取输出
      System.out.println(pingCommand);
      Process p = r.exec(pingCommand);
      if (p == null) {
        return false;
      }
      // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
      in = new BufferedReader(new InputStreamReader(p.getInputStream()));
      int connectedCount = 0;
      String line = null;
      while ((line = in.readLine()) != null) {
        connectedCount += getCheckResult(line);
      }
      // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
      return connectedCount == pingTimes;
    } catch (Exception ex) {
      ex.printStackTrace(); // 出现异常则返回假
      return false;
    } finally {
      try {
        in.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  /**
   * 停止运行ping
   */
  public static void killPing() {
    if (process != null) {
      process.destroy();
      Log.e(TAG, "process: " + process);
    }
  }
  public static void write(File file, String content) {
    BufferedWriter out = null;
    Log.e(TAG, "file: " + file);
    try {
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
      out.write(content);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  // 若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
  private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+line);
    Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
      return 1;
    }
    return 0;
  }
  /*
   * public static void main(String[] args) throws Exception { String ipAddress = "appdlssl.dbankcdn.com"; //
   * System.out.println(ping(ipAddress)); ping02(); // System.out.println(ping(ipAddress, 5, 5000)); }
   */
}

总结

到此这篇关于Android中实现ping功能的多种方法详解的文章就介绍到这了,更多相关android ping 功能内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_41648631/article/details/104921316

延伸 · 阅读

精彩推荐