英文:
How to get the latency from the ping command in java
问题
这段代码可以从服务器获取延迟,但我无法将其安全地保存在一个变量中。
我已经尝试过了,但不幸的是我在Java方面有点新手,无法解决这个问题。
如何将平均延迟以毫秒(ms)的形式保存在变量中?
谢谢帮助!
编辑:我需要平均延迟,所以我只需要“Mittelwert”的值 - 在这个例子中是值“25” - 保存在一个变量中。
所以我将J.Doe的代码添加到了我的代码中,并将“time”更改为“Mittelwert”,并且更改了
"String timeWithValue = s.split(" = ")[2];"
"String value = timeWithValue.split("ms")[0]"
所以这段代码需要插入到代码中,然后平均延迟以“Mittelwert”的形式将保存在变量“latency”中。
但是现在输出没有返回“平均延迟:25”。平均延迟根本没有返回。有人知道为什么会发生这种情况吗?
System.out.println("这里是平均延迟:\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);
}
}
输出:
这是命令的标准输出:
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
这是命令的标准错误(如果有的话):
这里是平均延迟:
<details>
<summary>英文:</summary>
with this code I get the latency from the server, but I am not able to safe it in a variable.
I already tried it, but unfortunatly I am kind of new in java and can't solve this problem.
How can I get the average latency in a variable in milliseconds (ms)?
Thanks for the help!
Edit: I need the average latency, so I just need the value of "Mittelwert" - in this example the value "25" in a variable.
So I added the code from J.Doe to my code and changed "time" to "Mittelwert" and changed
> "String timeWithValue = s.split(" = ")[2];"
>> "String value = timeWithValue.split("ms")[0]"
So this code need to get inserted in the code, then the average latency in form of "Mittelwert" will get safed in the variable "latency".
But now the output don't return "average latency: 25". The average latency don't get returned at all. Someone know why this happens?
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):
</details>
# 答案1
**得分**: 2
```java
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);
}
}
英文:
if your output is like
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
try this:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论