Nihil Nihil - 2 months ago 5
Java Question

How to check player's latency in java

How do I see how many milliseconds of ping a client has? I haven't tried anything yet because I don't have a clue what to do, if I search it online, I only get answers on how to ping in java, which is not what I need!


A ping / latency typically refers to the time it takes for an update packet to reach the destination (and back). So one way to determine the ping of a client from the server is to send some tiny packet from the client to the server with a field that looks something like this:

long sentMillis = System.currentTimeMillis();

which would contain the time in milliseconds when the packet was sent. Since you know when you got the packet on the server, capture current time in milliseconds:

long receivedMillis = System.currentTimeMillis();

Now, trivially:

// time from client to server
long ping = receivedMillis - sentMillis;