[英]How to get the latency from the ping command in java
使用此代碼,我可以從服務器獲得延遲,但我無法將其保存在變量中。
我已經嘗試過了,但不幸的是我是 Java 新手,無法解決這個問題。 如何以毫秒 (ms) 為單位獲得變量的平均延遲?
謝謝您的幫助!
編輯:我需要平均延遲,所以我只需要“Mittelwert”的值——在這個例子中,變量中的值“25”。 所以我將 J.Doe 的代碼添加到我的代碼中,並將“時間”更改為“Mittelwert”並更改
"String timeWithValue = s.split(" = ")[2];"
"字符串值 = timeWithValue.split("ms")[0]"
所以這段代碼需要插入到代碼中,然后“Mittelwert”形式的平均延遲將在變量“latency”中得到保護。
但現在輸出不會返回“平均延遲:25”。 根本不會返回平均延遲。 有人知道為什么會這樣嗎?
System.out.println("here is the average latency :\n");
double latency = 0.0;
while ((s = stdInput.readLine()) != null)
{
if (s.contains("Mittelwert")) {
String timeWithValue = s.split(" = ")[2];
String value = timeWithValue.split("ms")[0];
latency = Double.parseDouble(value);
System.out.println("latency " + latency);
}
}
Output:
Here is the standard output of the command:
Ping wird ausgef?hrt f?r 74.125.236.73 [2a00:1f78:fffd:9::d435:9868] mit 32 Bytes Daten:
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=26ms
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=29ms
Ping-Statistik f?r 2a00:1f78:fffd:9::d435:9868:
Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
(0% Verlust),
Ca. Zeitangaben in Millisek.:
Minimum = 25ms, Maximum = 29ms, Mittelwert = 26ms
Here is the standard error of the command (if any):
here is the average latency :
import java.io.*;
import java.util.*;
public class JavaPingExampleProgram
{
public static void main(String args[])
throws IOException
{
// create the ping command as a list of strings
JavaPingExampleProgram ping = new JavaPingExampleProgram();
List<String> commands = new ArrayList<String>();
commands.add("ping");
//commands.add("-c");
//commands.add("5");
commands.add("74.125.236.73");
ping.doCommand(commands);
}
public void doCommand(List<String> command)
throws IOException
{
String s = null;
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
}
}
Output:
Here is the standard output of the command:
Ping wird ausgef?hrt f?r 74.125.236.73 [2a00:1f78:fffd:9::d435:9868] mit 32 Bytes Daten:
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=26ms
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=24ms
Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
Ping-Statistik f?r 2a00:1f78:fffd:9::d435:9868:
Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
(0% Verlust),
Ca. Zeitangaben in Millisek.:
Minimum = 24ms, Maximum = 26ms, Mittelwert = 25ms
Here is the standard error of the command (if any):
如果你的輸出是這樣的
PING google.de (216.58.208.35): 56 data bytes
64 bytes from 216.58.208.35: icmp_seq=0 ttl=118 time=22.126 ms
64 bytes from 216.58.208.35: icmp_seq=1 ttl=118 time=27.877 ms
64 bytes from 216.58.208.35: icmp_seq=2 ttl=118 time=18.077 ms
64 bytes from 216.58.208.35: icmp_seq=3 ttl=118 time=33.099 ms
嘗試這個:
while ((s = stdInput.readLine()) != null)
{
if (s.contains("time")) {
String timeWithValue = s.split(" ")[6];
String value = timeWithValue.split("=")[1];
double latency = Double.parseDouble(value);
System.out.println(latency);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.