Android基础篇_ping实现

1.Ping是Windows、Unix和Linux系统下的一个命令。

ping也属于一个通信协议,是TCP/IP协议的一部分。

利用“ping”命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障。

Ping发送一个ICMP (Internet Control Messages Protocol)即因特网信报控制协议,回声请求消息给目的地并报告是否收到所希望的 ICMP  echo (ICMP回声应答),用来检查网络是否通畅或者网络连接速度的命令。广义来说即发送一个数据包,根据返回的数据包得到丢包率及平均时间得出网络的连接状态。
ping命令可以用在android中检测网络ip或者socket的连接,命令格式:ping ip地址(最简)
ping具有一些参数,可以具体定义包的个数、包的最大存活时间等

2.具体参数:

参数 详解
-a Audible ping.
-A 自适应ping,根据ping包往返时间确定ping的速度;
-b 允许ping一个广播地址;
-B 不允许ping改变包头的源地址;
-c count ping指定次数后停止ping; 
-d  使用Socket的SO_DEBUG功能;
-F flow_label 为ping回显请求分配一个20位的“flow label”,如果未设置,内核会为ping随机分配;
-f 极限检测,快速连续ping一台主机,ping的速度达到100次每秒;
-i interval 设定间隔几秒发送一个ping包,默认一秒ping一次;
-I interface 指定网卡接口、或指定的本机地址送出数据包;
-l preload 设置在送出要求信息之前,先行发出的数据包;
-L 抑制组播报文回送,只适用于ping的目标为一个组播地址
-n  不要将ip地址转换成主机名;
-p pattern 指定填充ping数据包的十六进制内容,在诊断与数据有关的网络错误时这个选项就非常有用,如:“-p ff”;
-q 不显示任何传送封包的信息,只显示最后的结果
-Q tos 设置Qos(Quality of Service),它是ICMP数据报相关位;可以是十进制或十六进制数,详见rfc1349和rfc2474文档;
-R 记录ping的路由过程(IPv4 only);
注意:由于IP头的限制,最多只能记录9个路由,其他会被忽略;
-r 忽略正常的路由表,直接将数据包送到远端主机上,通常是查看本机的网络接口是否有问题;如果主机不直接连接的网络上,则返回一个错误。
-S sndbuf Set socket sndbuf. If not specified, it is selected to buffer not more than one packet.
-s packetsize 指定每次ping发送的数据字节数,默认为“56字节”+“28字节”的ICMP头,一共是84字节;
包头+内容不能大于65535,所以最大值为65507(linux:65507, windows:65500);
-t ttl 设置TTL(Time To Live)为指定的值。该字段指定IP包被路由器丢弃之前允许通过的最大网段数;
-T timestamp_option 设置IP timestamp选项,可以是下面的任何一个:
  'tsonly' (only timestamps)
  'tsandaddr' (timestamps and addresses)
  'tsprespec host1 [host2 [host3]]' (timestamp prespecified hops). 
-M hint  设置MTU(最大传输单元)分片策略。
可设置为:
  'do':禁止分片,即使包被丢弃;
  'want':当包过大时分片;
  'dont':不设置分片标志(DF flag);
-m mark 设置mark;
-v 使ping处于verbose方式,它要ping命令除了打印ECHO-RESPONSE数据包之外,还打印其它所有返回的ICMP数据包;
-U  Print full user-to-user latency (the old behaviour).
Normally ping prints network round trip time, which can be different f.e. due to DNS failures.
-W timeout 以毫秒为单位设置ping的超时时间;
-w deadline deadline;
Android基础篇_ping实现_第1张图片
当然,这只是个小的例子,结合上面给的参数,你爱ping什么ping什么

3.Runtime.exec():

ping这个命令其实也没什么可说的,主要想再说说Runtime.exec()方法,这个方法不单纯只是Ping,其实他能完成几乎所有的Linux命令结果
还有Process类,顾名思义Process进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。
Android基础篇_ping实现_第2张图片
这个类,有获取读写流的方法,就说明我们刚才的exec会以流的方式返回结果。

3.1.Runtime

好,最后重点说一下Runtime,他在java.lang包下面,

/**
 * Executes the specified command and its arguments in a separate native
 * process. The new process inherits the environment of the caller. Calling
 * this method is equivalent to calling {@code exec(progArray, null, null)}.
 *
 * @param progArray
 *            the array containing the program to execute as well as any
 *            arguments to the program.
 * @return the new {@code Process} object that represents the native
 *         process.
 * @throws IOException
 *             if the requested program can not be executed.
 */
public Process exec(String[] progArray) throws java.io.IOException {
    return exec(progArray, null, null);
}

/**
 * Executes the specified command and its arguments in a separate native
 * process. The new process uses the environment provided in {@code envp}.
 * Calling this method is equivalent to calling
 * {@code exec(progArray, envp, null)}.
 *
 * @param progArray
 *            the array containing the program to execute as well as any
 *            arguments to the program.
 * @param envp
 *            the array containing the environment to start the new process
 *            in.
 * @return the new {@code Process} object that represents the native
 *         process.
 * @throws IOException
 *             if the requested program can not be executed.
 */
public Process exec(String[] progArray, String[] envp) throws java.io.IOException {
    return exec(progArray, envp, null);
}

/**
 * Executes the specified command and its arguments in a separate native
 * process. The new process uses the environment provided in {@code envp}
 * and the working directory specified by {@code directory}.
 *
 * @param progArray
 *            the array containing the program to execute as well as any
 *            arguments to the program.
 * @param envp
 *            the array containing the environment to start the new process
 *            in.
 * @param directory
 *            the directory in which to execute the program. If {@code null},
 *            execute if in the same directory as the parent process.
 * @return the new {@code Process} object that represents the native
 *         process.
 * @throws IOException
 *             if the requested program can not be executed.
 */
public Process exec(String[] progArray, String[] envp, File directory) throws IOException {
    // ProcessManager is responsible for all argument checking.
    return ProcessManager.getInstance().exec(progArray, envp, directory, false);
}

/**
 * Executes the specified program in a separate native process. The new
 * process inherits the environment of the caller. Calling this method is
 * equivalent to calling {@code exec(prog, null, null)}.
 *
 * @param prog
 *            the name of the program to execute.
 * @return the new {@code Process} object that represents the native
 *         process.
 * @throws IOException
 *             if the requested program can not be executed.
 */
public Process exec(String prog) throws java.io.IOException {
    return exec(prog, null, null);
}

/**
 * Executes the specified program in a separate native process. The new
 * process uses the environment provided in {@code envp}. Calling this
 * method is equivalent to calling {@code exec(prog, envp, null)}.
 *
 * @param prog
 *            the name of the program to execute.
 * @param envp
 *            the array containing the environment to start the new process
 *            in.
 * @return the new {@code Process} object that represents the native
 *         process.
 * @throws IOException
 *             if the requested program can not be executed.
 */
public Process exec(String prog, String[] envp) throws java.io.IOException {
    return exec(prog, envp, null);
}

/**
 * Executes the specified program in a separate native process. The new
 * process uses the environment provided in {@code envp} and the working
 * directory specified by {@code directory}.
 *
 * @param prog
 *            the name of the program to execute.
 * @param envp
 *            the array containing the environment to start the new process
 *            in.
 * @param directory
 *            the directory in which to execute the program. If {@code null},
 *            execute if in the same directory as the parent process.
 * @return the new {@code Process} object that represents the native
 *         process.
 * @throws IOException
 *             if the requested program can not be executed.
 */
public Process exec(String prog, String[] envp, File directory) throws java.io.IOException {
    // Sanity checks
    if (prog == null) {
        throw new NullPointerException("prog == null");
    } else if (prog.isEmpty()) {
        throw new IllegalArgumentException("prog is empty");
    }

    // Break down into tokens, as described in Java docs
    StringTokenizer tokenizer = new StringTokenizer(prog);
    int length = tokenizer.countTokens();
    String[] progArray = new String[length];
    for (int i = 0; i < length; i++) {
        progArray[i] = tokenizer.nextToken();
    }

    // Delegate
    return exec(progArray, envp, directory);
}

prog时需要执行的程序,怎么理解,其实很简单,就是系统的command命令,到这大家应该都清楚了,Android是Linux系统,这里的指令也就是Linux的命令了,例如ps(查看进程),ls,pwd(查看现在路径,文件等等)等等,大家都可以去试试。

*注意点:

    1、exec的String prog只能执行一条指令,如果需要执行多条指令需要放入sh脚本文件中执行或者调用多次exec函数。

    2、exec的String command不支持通配符(*)。

    3、有些指令需要指定File dir(工作目录)。

    4、默认的工作目录是根目录/。(通过执行指令pwd可查到)

    5、导出logcat日志到SD卡ziyouku目录,参数:("logcat -df logcat.log",new File("/mnt/sdcard/ziyouku/"))

    6、logcat读取权限:


好,今天就说这么多,最近在做Spingboot开发后台并发系统,所以写博客也是对之前项目的很多技术点的总结完善。



你可能感兴趣的:(Android基础篇_ping实现)