安卓實現Ping網段功能

一.什麼是Ping

Ping是Windows、Unix和Linux系統下的一個命令,ping也屬於一個通信協議,是TCP/IP協議的一部分。
利用“ping”命令可以檢查網絡是否連通,可以很好地幫助我們分析和判定網絡故障。
Ping發送一個ICMP(Internet Control Messages Protocol)即因特網信報控制協議,回聲請求消息給目的地並報告是否收到所希望的ICMP echo (ICMP回聲應答),用來檢查網絡是否通暢或者網絡連接速度的命令。廣義來說即發送一個數據包,根據返回的數據包得到丟包率及平均時間得出網絡的連接狀態。
ping命令可以用在android中檢測網絡ip或者socket的連接,命令格式:ping ip地址(最簡)
ping具有一些參數,可以具體定義包的個數、包的最大存活時間等。

二.具體參數

三.代碼實現

/*測試Ping ip地址 無法ping:192.155.1.14 可以:192.168.1.41*/
new Ping().execute("www.baidu.com");
    public class Ping extends AsyncTask<String, Boolean, Boolean> {
        @Override
        protected Boolean doInBackground(String... strings) {
            try {
                Process p = Runtime.getRuntime().exec("ping -c 1 -w 3 " + strings[0]);
                InputStream input = p.getInputStream();
                InputStreamReader isr = new InputStreamReader(input);
                BufferedReader br = new BufferedReader(isr);
                String line;
                StringBuilder builder = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    builder.append(line);
                }
                br.close();
                isr.close();
                input.close();
                br.close();
                Logs.i("返回的數據:"+builder+"  對比:"+builder.toString().contains("ttl"));
                /*如果Ip地址Ping成功後數據裏面會有ttl這個數據,所以對比這個字符串即可*/
                return builder.toString().contains("ttl");
            } catch (MalformedURLException e) {
                Logs.e(e.toString());
                return false;
            } catch (IOException e) {
                Logs.e(e.toString());
                return false;
            }
        }


        @Override
        protected void onPostExecute(Boolean isPing) {
            Logs.v("Ping的結果:" + isPing);
            if(isPing){
                /*設備Ip地址ping成功後向後臺發送數據*/
                if (!TextUtils.isEmpty(cllx)) {
                    if (INSPECT_START.equals(cllx)) {
                        //查驗開始
                        inspectStart();
                    } else if (INSPECT_END.equals(cllx)) {
                        //查驗結束
                        inspectEnd();
                    }
                }
//                DialogNoticeUtil.show(RequestAction.this,"執法記錄儀連接成功");
            }else {
                DialogNoticeUtil.show(RequestAction.this,"執法記錄儀連接失敗");
            }
        }
    }

四.日誌打印數據

2019-12-17 16:16:27.615 10436-10454/? I/lcb: 返回的數據:PING 192.155.1.14 (192.155.1.14) 56(84) bytes of data.--- 192.155.1.14 ping statistics ---3 packets transmitted, 0 received, 100% packet loss, time 2004ms

 對比:false
2019-12-17 16:28:26.812 11066-11084/com.tmri.enforcement.app I/lcb: 返回的數據:PING www.a.shifen.com (183.232.231.172) 56(84) bytes of data.64 bytes from localhost (183.232.231.172): icmp_seq=2 ttl=56 time=20.6 ms--- www.a.shifen.com ping statistics ---2 packets transmitted, 1 received, 50% packet loss, time 1005msrtt min/avg/max/mdev = 20.636/20.636/20.636/0.000 ms  

對比:true
 

發佈了236 篇原創文章 · 獲贊 48 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章