synchronizing computers in a network
Synchronizing computers seems to me essential in every network game; however i haven''t found an article that explains how to do it. For example, I need to know whether some event in computer A happened before some event in computer B. As I don''t know how long it takes a packet to be transmitted between the computers, I can''t figure out a way to do it. Any idea?
If you really need to keep track of such things you could send a timestamp along with the data sent. That means, you have computer A send its local time along with the event.
When computer B receives the packet, it knows when the event has occured on computer A (assuming that both have the same base for time ...).
If they do not have the same base (which most likely they won''t have), you could define some sort of "virtual" clock (like number of milliseconds since connection was established ...). this way you can make sure that differences like GMT+1, GMT+5 etc. won''t have a negative impact.
Of course, this system also doesn''t work totally accurate, but I think it should be fairly enough for a game...
Hope that helped!
Indeterminatus
--si tacuisses, philosophus mansisses--
When computer B receives the packet, it knows when the event has occured on computer A (assuming that both have the same base for time ...).
If they do not have the same base (which most likely they won''t have), you could define some sort of "virtual" clock (like number of milliseconds since connection was established ...). this way you can make sure that differences like GMT+1, GMT+5 etc. won''t have a negative impact.
Of course, this system also doesn''t work totally accurate, but I think it should be fairly enough for a game...
Hope that helped!
Indeterminatus
--si tacuisses, philosophus mansisses--
Indeterminatus--si tacuisses, philosophus mansisses--
I agree that the best way is to keep a clock on both computers. It''s also important that you synchronize them periodically to avoid problems like the ones that appeared in multiplayer Descent.. players would sometimes wind up with completely different scores.
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
Check out the resources section of this page, as well as www.gamasutra.com (you''ll have to scroll way down the articles list to articles from 2000 or 2002 I believe)
*Lots* of great stuff to be read just sitting and waiting for your eyes.
I have recently spent a lot of time re-reading all the articles I have collected over the last four or so years and I have found it quite informative (its surprising how much info you can get from articles you have already read!)
Anyway, good luck.
- Jacob
*Lots* of great stuff to be read just sitting and waiting for your eyes.
I have recently spent a lot of time re-reading all the articles I have collected over the last four or so years and I have found it quite informative (its surprising how much info you can get from articles you have already read!)
Anyway, good luck.
- Jacob
"1 is equal to 2 for significantly large quantities of 1" - Anonymous
If there was some universal clock that both computers use, of course there would be no problem. For example, if I could synchronize both computers to UTC (but it has to be very accurate), there would be no problem. In this case the question is - how do I get UTC in a very good accuracy?
But i''m sure there has to be some decent alternative. If I wanna play a game in a LAN which isn''t connected to the internet, UTC is no help. I can''t just start a virtual clock when the connection between the computers is established, because the computers won''t know at the same time that a connection was established. In this case the question is still - what do I do?
But i''m sure there has to be some decent alternative. If I wanna play a game in a LAN which isn''t connected to the internet, UTC is no help. I can''t just start a virtual clock when the connection between the computers is established, because the computers won''t know at the same time that a connection was established. In this case the question is still - what do I do?
You could syncronise all the clients to the server clock.
To obtain the clock you could do this:
First, send a packet request for time sync with the server.
Save the time where this packet was sent.
Then when you receive the packet from the server with his clock time, you check at what time you received it.
You substract the time you received it with the time you sent the first packet and you divide by 2.
Then you substract this from the server clock time you received in the server packet.
This will give you the server clock time, but I believe there might be a difference of some milliseconds between the real server clock time and the clock time you have calculated.
So you could repeat this 4-5 time and do an average to find a more accurate time...
I never tested it, it''s just an idea and it might suck...
To obtain the clock you could do this:
First, send a packet request for time sync with the server.
Save the time where this packet was sent.
Then when you receive the packet from the server with his clock time, you check at what time you received it.
You substract the time you received it with the time you sent the first packet and you divide by 2.
Then you substract this from the server clock time you received in the server packet.
This will give you the server clock time, but I believe there might be a difference of some milliseconds between the real server clock time and the clock time you have calculated.
So you could repeat this 4-5 time and do an average to find a more accurate time...
I never tested it, it''s just an idea and it might suck...
![](tongue.gif)
Thanx Hedos. For now this is the only solution I have; I wonder if this is what real games do. I hope that the time it takes a packet to get from computer A to computer B is the same as the time it takes a packet to get from computer B to computer A, otherwise it''s no good to divide in 2...
Kaludas - is NTP good only for synchronizing with time server or also synchronize between compures in a LAN/WAN? And do u know any friendly tutorials about using it?
In order to synchronize machines on a LAN, you define one as a server, and slave the others off that. Bonus points if you get the server to in turn sychronize off some higher-tier time server, so they''re all in sync with the world, but that''s not strictly necessary.
enum Bool { True, False, FileNotFound };
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement