Nihil
Nihil

Reputation: 37

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!

Upvotes: 0

Views: 804

Answers (1)

AlmasB
AlmasB

Reputation: 3407

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;

Upvotes: 2

Related Questions