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

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

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&#39;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 &quot;Mittelwert&quot; - in this example the value &quot;25&quot; in a variable.
So I added the code from J.Doe to my code and changed &quot;time&quot; to &quot;Mittelwert&quot; and changed  

&gt; &quot;String timeWithValue = s.split(&quot; = &quot;)[2];&quot; 
&gt;&gt; &quot;String value = timeWithValue.split(&quot;ms&quot;)[0]&quot;

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;.
 
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?



     System.out.println(&quot;here is the average latency :\n&quot;);
    double latency = 0.0;
    while ((s = stdInput.readLine()) != null)
    {
        if (s.contains(&quot;Mittelwert&quot;)) {
            String timeWithValue = s.split(&quot; = &quot;)[2];
            String value = timeWithValue.split(&quot;ms&quot;)[0];
            latency = Double.parseDouble(value);
            System.out.println(&quot;latency &quot; + 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&lt;String&gt; commands = new ArrayList&lt;String&gt;();
        commands.add(&quot;ping&quot;);
        //commands.add(&quot;-c&quot;);
        //commands.add(&quot;5&quot;);
        commands.add(&quot;74.125.236.73&quot;);
        ping.doCommand(commands);
      }
    
      public void doCommand(List&lt;String&gt; 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(&quot;Here is the standard output of the command:\n&quot;);
        while ((s = stdInput.readLine()) != null)
        {
          System.out.println(s);
        }
    
        // read any errors from the attempted command
        System.out.println(&quot;Here is the standard error of the command (if any):\n&quot;);
        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(&quot;time&quot;)) {
                String timeWithValue = s.split(&quot; &quot;)[6];
                String value = timeWithValue.split(&quot;=&quot;)[1];
                double latency = Double.parseDouble(value);
                System.out.println(latency);
            }

        }

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:

确定