ping通的报文:
ping -c 3 -W 1 127.0.0.5
PING 127.0.0.5 (127.0.0.5) 56(84) bytes of data.
64 bytes from 127.0.0.5: icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from 127.0.0.5: icmp_seq=2 ttl=64 time=0.077 ms
64 bytes from 127.0.0.5: icmp_seq=3 ttl=64 time=0.069 ms--- 127.0.0.5 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2042ms
rtt min/avg/max/mdev = 0.069/0.083/0.105/0.018 ms
不通的报文:
ping -c 3 -W 1 192.168.0.5
PING 192.168.0.5 (192.168.0.5) 56(84) bytes of data.--- 192.168.0.5 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2021ms
- private fun ping(ip:String): Boolean {
-
- val arr = arrayOf("sh", "-c", "ping -c 2 -W 1 $ip")//192.168.0.1
-
- val process = Runtime.getRuntime().exec(arr)
- val input = BufferedReader(InputStreamReader(process.inputStream));
- var connectedCount = 0
- var line: String? = null
- input.use {
- try {
- while (input.readLine().also { line = it } != null) {
- val result = getCheckResult(line!!)
- connectedCount += result
-
- LogHelper.d("MainActivity --- connectedCount = $connectedCount result = $result line = $line")
- if(connectedCount > 0){
- return true;
- }
- }
- }catch (e:Exception){
- e.printStackTrace()
- return false
- }
-
- }
-
- return connectedCount > 0
-
- }
-
-
- private fun getCheckResult(line: String): Int {
- return if ((line.contains("ttl=") || line.contains("TTL=")) && line.contains("ms")) {
- 1
- } else 0
- }
在此 做个笔记