有一种方法可以在 java 中测量延迟网络

2022-01-19 00:00:00 networking network-programming java

我正在使用一个简单的程序,一开始我将在所有 MAN 主机上执行 ping 操作,以验证是否所有主机都在线(完整),但我想实现一些测量主机之间延迟的方法.有没有办法做到这一点?有什么建议吗?

I'm working in a simple program, for beginning i will ping in all MAN hosts to verify if all hosts are online (complete) but i want implement some way that measure latency between hosts. Is there any way to do that? Any tips?

无论如何,谢谢

推荐答案

您可以保留 pingpong 打包的时间戳,并简单地计算两者之间的差异两个.

You can keep a timestamp for the ping and the pong packed and simply compute the difference between the two.

根据定义,延迟

您可以多次重复该过程以计算其他指标,例如抖动.

You can repeat the process more that one time to compute other metric, such as the jitter.

以下简单的东西应该可以达到目的.

Something simple as the following should serve the purpose.

while (sentPacket < MAX_PACK_NUM) {
            // Timestamp in ms when we send it
            Date now = new Date();
            long msSend = now.getTime();
             ....
            //send ping
            socket.send(ping);
            //receive ping
            socket.receive(response);
            now = new Date();
            long msReceived = now.getTime();
            // Print the packet and the delay
            long latency= msReceived - msSend;
            ++sentPacket;
         }

相关文章