Android 解析ping回复包

目标:

已知多个字符串,形如:"64 bytes from 39.156.66.18: icmp_seq=1 ttl=50 time=29.7 ms"

获取 总Ping时间(XX:XX:XX格式),最小时间,最大时间,平均时间,丢包率,已发送个数,已接收个数

示例程序:

java 复制代码
import java.util.regex.Matcher;
import java.util.regex.Pattern;

private class PingThread extends Thread {
    // ...

    // 使用正则表达式匹配,从ping输出中提取特定值
    private static final Pattern PING_PATTERN = Pattern.compile("time=(\\d+\\.\\d+) ms");
    private static final Pattern PACKET_LOSS_PATTERN = Pattern.compile("(\\d+)% packet loss");
    private static final Pattern PACKETS_SENT_PATTERN = Pattern.compile("(\\d+) packets transmitted");
    private static final Pattern PACKETS_RECEIVED_PATTERN = Pattern.compile("(\\d+) packets received");

    @Override
    public void run() {
        super.run();

        // ... 

        int packetsSent = 0;
        int packetsReceived = 0;
        float packetLoss = 0.0f;
        long minPingTime = Long.MAX_VALUE;
        long maxPingTime = Long.MIN_VALUE;
        long totalPingTime = 0;

        try {
            // ... 

            // 读取标准输入流
            while ((line = successReader.readLine()) != null) {
                System.out.println("Message: " + line);  // 在控制台看一下消息

                Matcher packetLossMatcher = PACKET_LOSS_PATTERN.matcher(line);
                if (packetLossMatcher.find()) {
                    packetLoss = Float.parseFloat(packetLossMatcher.group(1));
                }

                Matcher packetsSentMatcher = PACKETS_SENT_PATTERN.matcher(line);
                if (packetsSentMatcher.find()) {
                    packetsSent = Integer.parseInt(packetsSentMatcher.group(1));
                }

                Matcher packetsReceivedMatcher = PACKETS_RECEIVED_PATTERN.matcher(line);
                if (packetsReceivedMatcher.find()) {
                    packetsReceived = Integer.parseInt(packetsReceivedMatcher.group(1));
                }

                Matcher pingMatcher = PING_PATTERN.matcher(line);
                while (pingMatcher.find()) {
                    float pingTime = Float.parseFloat(pingMatcher.group(1));
                    totalPingTime += pingTime;
                    minPingTime = Math.min(minPingTime, (long) pingTime);
                    maxPingTime = Math.max(maxPingTime, (long) pingTime);
                }

                // ...
            }

            // 计算平均Ping时间
            float avgPingTime = totalPingTime / (float) packetsReceived;
            
            // 如果需要可以把ms值转化为 "XX:XX:XX" 格式.
            // 例如:
            String formattedMinPingTime = formatTime(minPingTime);
            String formattedMaxPingTime = formatTime(maxPingTime);
            String formattedAvgPingTime = formatTime((long) avgPingTime);

            // ...

        } catch (IOException | InterruptedException e) {
            // 处理异常
            e.printStackTrace();
        } finally {
            // ...
        }
    }

    // ms值转换为"XX:XX:XX"格式
    private String formatTime(long milliseconds) {
        long seconds = milliseconds / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        return String.format("%02d:%02d:%02d", hours, minutes % 60, seconds % 60);
    }
}
相关推荐
Amarantine、沐风倩✨6 分钟前
设计一个监控摄像头物联网IOT(webRTC、音视频、文件存储)
java·物联网·音视频·webrtc·html5·视频编解码·七牛云存储
li_liuliu8 分钟前
Android4.4 在系统中添加自己的System Service
android
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien1 小时前
Spring Boot常用注解
java·spring boot·后端
gywl2 小时前
openEuler VM虚拟机操作(期末考试)
linux·服务器·网络·windows·http·centos
WTT00112 小时前
2024楚慧杯WP
大数据·运维·网络·安全·web安全·ctf
苹果醋32 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
C4rpeDime2 小时前
自建MD5解密平台-续
android
Hello.Reader3 小时前
深入解析 Apache APISIX
java·apache
杨德杰3 小时前
QT网络(一):主机信息查询
网络·qt