如何从Java中的ping命令获取延迟。

huangapple go评论101阅读模式
英文:

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”。平均延迟根本没有返回。有人知道为什么会发生这种情况吗?

  1. System.out.println("这里是平均延迟:\n");
  2. double latency = 0.0;
  3. while ((s = stdInput.readLine()) != null)
  4. {
  5. if (s.contains("Mittelwert")) {
  6. String timeWithValue = s.split(" = ")[2];
  7. String value = timeWithValue.split("ms")[0];
  8. latency = Double.parseDouble(value);
  9. System.out.println("延迟 " + latency);
  10. }
  11. }

输出:
这是命令的标准输出:

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
这是命令的标准错误(如果有的话):

这里是平均延迟:

  1. <details>
  2. <summary>英文:</summary>
  3. with this code I get the latency from the server, but I am not able to safe it in a variable.
  4. I already tried it, but unfortunatly I am kind of new in java and can&#39;t solve this problem.
  5. How can I get the average latency in a variable in milliseconds (ms)?
  6. Thanks for the help!
  7. Edit: I need the average latency, so I just need the value of &quot;Mittelwert&quot; - in this example the value &quot;25&quot; in a variable.
  8. So I added the code from J.Doe to my code and changed &quot;time&quot; to &quot;Mittelwert&quot; and changed
  9. &gt; &quot;String timeWithValue = s.split(&quot; = &quot;)[2];&quot;
  10. &gt;&gt; &quot;String value = timeWithValue.split(&quot;ms&quot;)[0]&quot;
  11. So this code need to get inserted in the code, then the average latency in form of &quot;Mittelwert&quot; will get safed in the variable &quot;latency&quot;.
  12. But now the output don&#39;t return &quot;average latency: 25&quot;. The average latency don&#39;t get returned at all. Someone know why this happens?
  13. System.out.println(&quot;here is the average latency :\n&quot;);
  14. double latency = 0.0;
  15. while ((s = stdInput.readLine()) != null)
  16. {
  17. if (s.contains(&quot;Mittelwert&quot;)) {
  18. String timeWithValue = s.split(&quot; = &quot;)[2];
  19. String value = timeWithValue.split(&quot;ms&quot;)[0];
  20. latency = Double.parseDouble(value);
  21. System.out.println(&quot;latency &quot; + latency);
  22. }
  23. }
  24. Output:
  25. Here is the standard output of the command:
  26. Ping wird ausgef?hrt f?r 74.125.236.73 [2a00:1f78:fffd:9::d435:9868] mit 32 Bytes Daten:
  27. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=26ms
  28. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
  29. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
  30. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=29ms
  31. Ping-Statistik f?r 2a00:1f78:fffd:9::d435:9868:
  32. Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
  33. (0% Verlust),
  34. Ca. Zeitangaben in Millisek.:
  35. Minimum = 25ms, Maximum = 29ms, Mittelwert = 26ms
  36. Here is the standard error of the command (if any):
  37. here is the average latency :
  38. import java.io.*;
  39. import java.util.*;
  40. public class JavaPingExampleProgram
  41. {
  42. public static void main(String args[])
  43. throws IOException
  44. {
  45. // create the ping command as a list of strings
  46. JavaPingExampleProgram ping = new JavaPingExampleProgram();
  47. List&lt;String&gt; commands = new ArrayList&lt;String&gt;();
  48. commands.add(&quot;ping&quot;);
  49. //commands.add(&quot;-c&quot;);
  50. //commands.add(&quot;5&quot;);
  51. commands.add(&quot;74.125.236.73&quot;);
  52. ping.doCommand(commands);
  53. }
  54. public void doCommand(List&lt;String&gt; command)
  55. throws IOException
  56. {
  57. String s = null;
  58. ProcessBuilder pb = new ProcessBuilder(command);
  59. Process process = pb.start();
  60. BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
  61. BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  62. // read the output from the command
  63. System.out.println(&quot;Here is the standard output of the command:\n&quot;);
  64. while ((s = stdInput.readLine()) != null)
  65. {
  66. System.out.println(s);
  67. }
  68. // read any errors from the attempted command
  69. System.out.println(&quot;Here is the standard error of the command (if any):\n&quot;);
  70. while ((s = stdError.readLine()) != null)
  71. {
  72. System.out.println(s);
  73. }
  74. }
  75. }
  76. Output:
  77. Here is the standard output of the command:
  78. Ping wird ausgef?hrt f?r 74.125.236.73 [2a00:1f78:fffd:9::d435:9868] mit 32 Bytes Daten:
  79. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=26ms
  80. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
  81. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=24ms
  82. Antwort von 2a00:1f78:fffd:9::d435:9868: Zeit=25ms
  83. Ping-Statistik f?r 2a00:1f78:fffd:9::d435:9868:
  84. Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
  85. (0% Verlust),
  86. Ca. Zeitangaben in Millisek.:
  87. Minimum = 24ms, Maximum = 26ms, Mittelwert = 25ms
  88. Here is the standard error of the command (if any):
  89. </details>
  90. # 答案1
  91. **得分**: 2
  92. ```java
  93. while ((s = stdInput.readLine()) != null)
  94. {
  95. if (s.contains("time")) {
  96. String timeWithValue = s.split(" ")[6];
  97. String value = timeWithValue.split("=")[1];
  98. double latency = Double.parseDouble(value);
  99. System.out.println(latency);
  100. }
  101. }
英文:

if your output is like

  1. PING google.de (216.58.208.35): 56 data bytes
  2. 64 bytes from 216.58.208.35: icmp_seq=0 ttl=118 time=22.126 ms
  3. 64 bytes from 216.58.208.35: icmp_seq=1 ttl=118 time=27.877 ms
  4. 64 bytes from 216.58.208.35: icmp_seq=2 ttl=118 time=18.077 ms
  5. 64 bytes from 216.58.208.35: icmp_seq=3 ttl=118 time=33.099 ms

try this:

  1. while ((s = stdInput.readLine()) != null)
  2. {
  3. if (s.contains(&quot;time&quot;)) {
  4. String timeWithValue = s.split(&quot; &quot;)[6];
  5. String value = timeWithValue.split(&quot;=&quot;)[1];
  6. double latency = Double.parseDouble(value);
  7. System.out.println(latency);
  8. }
  9. }

huangapple
  • 本文由 发表于 2020年8月25日 05:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63568682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定