#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    76
    Rep Power
    23

    Server/client sync issues


    I'm working on a multiplayer network game and a part of it includes a 2d area of space with objects moving around in various directions (x/y) with various speeds, asteroids, ships, etc...

    to cut down on network traffic I decided the client & server should keep track of object positions independently and the server would just update the clients every 5 seconds or so.

    the problem is I can't seem to get the client & server to ever be in sync with each other so when the client gets the update from the server the objects jump back or forward and there's never smooth movement

    I'm using a timer with a 10ms interval to move objects on the client and a similar timer with the same interval on the server but on the server side nothing is actually drawn, all that happens is that x/y coordinates of objects are updated in memory

    Can anyone tell me a few tricks I can try?
  2. #2
  3. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    422
    First off welcome to devshed. Second, what language are you making the game with? Without knowing this it is kind of hard to diagnose. Also, have you tested it with multiple connections yet? There could be a problem somewhere in this part of the code. Impossible to tell without more info. What I would suggest is throwing in some code to see how much extra time it is actually taking to render the graphics. Because the server is just "crunching numbers" it should be almost instant on any modern CPU. But throw in the GFx on the client side, and even a few milliseconds could throw it off. If this is the case, you might try threading the x,y coordinates and graphics independently so it isn't stuck waiting for the GFx to render or something. Also, you might try setting the 'sync rate' a little higher than once every 5 seconds just to test it. See if it makes a greater difference with different pause times. This could help optimize the game, even if it produces minor lag.Someone with more experience will probably be able to help you more.....

    Comments on this post

    • Viper_SB agrees : very nice info
    Last edited by tagmanadvance; December 31st, 2006 at 12:05 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    76
    Rep Power
    23
    i'm making it in vb6
    i think i have a solution but i need to sleep before fully implementing it

    so far I've only been running one client at a time and it's all localhost so network lag isn't the issue yet

    i set the update interval to 50ms which at first was way too much data but i streamlined it by making a special protocol for server to client messages that would be sent in high volume, using the bare minimum number of bytes. and also only updating positions of objects that someone is observing. the game is split into 294 different "sectors" of space and if no one is observing a sector then no movements are made in it

    the real trick, which i'll try tomorrow would be to have the client adjust the interval for the timer which moves the objects. it could take the fastest moving object in the sector (which should have the largest discrepency), and say.. if I thought it was at this location but in reality it's in this location then that means I'm behind, (or ahead) and then it can speed up or slow down the timer until the discrepency between the two locations is within an acceptable limit.

    I'm pretty sure this will work because I've added a slider in the game to manually adjust the interval and i can always get a comfortable setting but it's always changing based on how many objects, how fast they're moving, how many collisions etc... so an auto-adjusting method should do the trick.

    thanks for the response btw
  6. #4
  7. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    422
    That is a good solution I think. But there is something that is bugging me about it. Will the "auto adjuster" adjust to just you, or to everyone? Because the way a server should work is to gather info from multiple connections, then resend that info to all connected clients. Otherwise it would just be a p2p game. If you change the sync rate for any given "sector" to optimize performance for you, what about other connected players in different "sectors"?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    76
    Rep Power
    23
    Originally Posted by tagmanadvance
    That is a good solution I think. But there is something that is bugging me about it. Will the "auto adjuster" adjust to just you, or to everyone? Because the way a server should work is to gather info from multiple connections, then resend that info to all connected clients. Otherwise it would just be a p2p game. If you change the sync rate for any given "sector" to optimize performance for you, what about other connected players in different "sectors"?
    well the server's timer interval will remain constant so the client just adjusts it's to try to stay in sync with the server, so things are moving around at the same rate for all players, and each client is attempting to show that acuratly

    i just tried it and it sort of works but when there's too many objects in the sector it still gets a little glitchy so it remains to be seen if i can get this method to work perfectly
  10. #6
  11. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    422
    I have very little experience with this, but I'll be doing the exact same thing soon, except it will be an RPG, and in Java. Now, answer me this, are you worried about hackers? Because if you aren't then just reverse engineer what you have already done. Have the clients handle the load and the server be updated. I think this would work, but I really cant say. That way the server would just act as a 'middle man' that sends out updated x,y,z coordinates as needed. If you are worried about hackers, then this is definitely out of the question.

    Now, I cannot stress enough that I myself am a nub at this, so don't waste your time doing anything big if there is a large possibility it won't work. I would post this in the VB section for more specialized help.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    76
    Rep Power
    23
    Originally Posted by tagmanadvance
    I have very little experience with this, but I'll be doing the exact same thing soon, except it will be an RPG, and in Java. Now, answer me this, are you worried about hackers? Because if you aren't then just reverse engineer what you have already done. Have the clients handle the load and the server be updated. I think this would work, but I really cant say. That way the server would just act as a 'middle man' that sends out updated x,y,z coordinates as needed. If you are worried about hackers, then this is definitely out of the question.

    Now, I cannot stress enough that I myself am a nub at this, so don't waste your time doing anything big if there is a large possibility it won't work. I would post this in the VB section for more specialized help.
    ya i am worried about hackers so the server has to have the ultimate say on where objects are and what they're doing.

    i havn't gotten it working quite right yet but it's starting to look like it might be better to have a speed modifier rather than adjusting the timer, so like when an object moves it would be like
    myX = myX + movX + syncModifier
    where movX is the speed (on the X axis) the object is supposed to be moving.
    i'm not sure yet, i'm sure almost any network game has had this issue so there's probably dozens of solutions out there.
    I may check out the vb forum but I thought I might get more responses in a general forum since the problem isn't really specific to VB and I can understand other languages well enough that if someone were to paste some C code I would be able to figure it out.

    if i come up with a solution that works well, or if I steel a solution that works well I'll post it here in this thread so you can use it in your project.
  14. #8
  15. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    422
    Thanks. BTW I Found THIS .
    Looks like it may be a possible solution, and looks easy to integrate.
    Last edited by tagmanadvance; January 2nd, 2007 at 06:15 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    76
    Rep Power
    23
    Originally Posted by tagmanadvance
    Thanks. BTW I Found THIS .
    Looks like it may be a possible solution, and looks easy to integrate.
    thanks actually that brings up the same point a friend of mine said
    vb6 dosen't seem to have an absolute microtime i can reference too, I have "time" and "time$" but the best I can do there is seconds, not milliseconds. its possible the shifting and jumping around of objects is because by the time the client gets the message about where an object should be, it's actually where that object should have been around 200-300 ms ago. and without synced clocks it's impossible to calculate it's true posision

    the client & server both have a "stardate" variable which is incremented by .01 every 10ms by a timer and i've also made a lag check system where the server sends an updated stardate to the client, the client takes it, sets it, and then replies, and the server takes the difference between the stardate that was sent and the stardate at which the reply was received and calls that the lag for that client, but so far the lag seems to be only .01 - .02 "stardates" so either I'm not getting an accurate measurement of lag or something

    it would be a lot easier if I could just get an absolute time in ms which wasn't being controlled by a timer but is a system variable lsimilar to time$

    i could have sworn there was one and I've used it before but maybe that was a different language and i'm getting confused :/
  18. #10
  19. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    422
    That would be one of those things to ask about in the vb sectiion.
    "Java makes impossible things possible, but makes easy things difficult." - Somebody
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    76
    Rep Power
    23
    okay looks like i've gotten through this problem, in vb i was able to use the Timer variable which is acurate enough. but also read only, it's the number of milliseconds that have passed since midnight.

    the solution was in getting 4 more peices of information:
    what time is it on the client?
    what time is it on the server?
    what is the difference between those two?
    and how long does it take for a message the server sends to get to the client?

    once the game client has this, and if the server adds a timestamp to each object position update, the client can calculate where the object is supposed to be now. seems to work well and also lets me get away with fewer updates while maintaining a smooth and acurate simulation

    thanks for the help all it was very useful
  22. #12
  23. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    422
    Sorry to bring up an old thread, but I'm curious, is a demo ready? I'd really like to see how it turned out =D
    "Java makes impossible things possible, but makes easy things difficult." - Somebody

IMN logo majestic logo threadwatch logo seochat tools logo