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);
    }
}
相关推荐
Yeats_Liao13 分钟前
Spring 框架:配置缓存管理器、注解参数与过期时间
java·spring·缓存
Yeats_Liao13 分钟前
Spring 定时任务:@Scheduled 注解四大参数解析
android·java·spring
码明13 分钟前
SpringBoot整合ssm——图书管理系统
java·spring boot·spring
某风吾起17 分钟前
Linux 消息队列的使用方法
java·linux·运维
xiao-xiang20 分钟前
jenkins-k8s pod方式动态生成slave节点
java·kubernetes·jenkins
取址执行32 分钟前
Redis发布订阅
java·redis·bootstrap
S-X-S1 小时前
集成Sleuth实现链路追踪
java·开发语言·链路追踪
快乐就好ya1 小时前
xxl-job分布式定时任务
java·分布式·spring cloud·springboot
沉默的煎蛋1 小时前
MyBatis 注解开发详解
java·数据库·mysql·算法·mybatis
Aqua Cheng.1 小时前
MarsCode青训营打卡Day10(2025年1月23日)|稀土掘金-147.寻找独一无二的糖葫芦串、119.游戏队友搜索
java·数据结构·算法