You can keep a timestamp for the ping
and the pong
packed and simply compute the difference between the two.
This is by definition, latency
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;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…