Server/Client Time Synchronization?

Started by
2 comments, last by wodinoneeye 10 years, 6 months ago

How can I properly synchronize time between client/server and all other clients?

For example, say I have a server broadcast command to all clients like "start match".

Obviously, you want all clients to start at the same time... But if you simply, say, start the match on the client... 10 seconds after receiving the server command... There will be variation between clients of when they receive the command based on how good their connection is and how many times the packet may be lost or have to be resent

Advertisement

Typically you would send a ping message every n-seconds. When the server receives the packet, it will impart the server time (in tick form) and send a response back to the client. The client would then know the following:

1: The time the packet was created by the client in ticks

2: The time the server received the packet

3: The time the client received the packet

Then using that information you can determine the latency between the client and the server.

When the client receives the ping message they can get the server time (from the message), then add the latency to get "actual" server time.

You could then store that initial server time, and client / server time difference into a variable on the client. Then using a property automatically get the "actual" server time by then adding the average latency.

(not real code)


public DateTime serverTime
{
   get {
     return clientTime + serverDifference;
   }
}

Hopefully this gives you an idea on how to get started doing this!

You cannot be 100% "in sync" because of the law of relativity. Also, because of network lag.

That doesn't matter. All you need is for events to happen in the same order and at the same speed on all machines.

enum Bool { True, False, FileNotFound };

I did something like this with my own reliable delivery protocol built ontop of UDP. The ping mechanism was at a low level built right into the network packet processing thread (client and server). An incoming ping was processed immediately and the reply (pong?) pushed out the socket immediately to eliminate queueing delays. The round trip time was tracked for the connection to be used for various things like game clock sync adjustments and also for signaling the game's Application level of slowdowns in communications (so compensation/adjustment could be made at that level to game traffic being generated - like priority near details and slowing updates of further away details).

That project was for a large scale server cluster (lots of processing nodes for one game world) with ALOT of packet traffic within the server itself as well as out to multiple clients. That meant that a slowdown to the clients was signalled around the cluster (more low level priority signalling). Various other special traffic was made to do keep-alives and low level background file transfers.

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

This topic is closed to new replies.

Advertisement