5

Recently I have been working on a multiplayer networking app. I had successfully implemented the client server model using Bonjour services and NSStream objects. The two way communication has been done successfully.

The problem I'm facing is: the two devices running the app are connected via local wifi. There is a latency problem while sending the messages from client to server and vice versa.

Describing in brief:

  • It's a car racing game with at most two players.
  • One server and other client.
  • When client press the buttons a particular message is sent to the server telling which key or button was pressed. Then the server responds accordingly and make changes on the client's car on the server itself. These changes are not always the same. There is always a difference between the actual location of the car on the client's screen and that in the server screen.

Any ideas?

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
Tornado
  • 1,069
  • 12
  • 28
  • Maybe instead of sending key presses to the server you should only send the state of the car to the server. like speed, x,y,z position. This way there won't be lag on the client machine, but there may be inconsistencies between server and client. – netbrain Apr 29 '11 at 13:25
  • 1
    possible duplicate of [Dealing with Latency in Networked Games](http://stackoverflow.com/questions/42515/dealing-with-latency-in-networked-games) – Brian Roach Apr 29 '11 at 13:25
  • 6
    @netbrain - oh dear god no. Rule #1 *never trust the client* – Brian Roach Apr 29 '11 at 13:26
  • 1
    @netbrain: This is actually makes synchronization more difficult as you add more players... and, as @Brian Roach says, never trust the client! The server should be the ultimate authority – KTF Apr 29 '11 at 13:28
  • then the player playing on the server will always have the advantage? – netbrain Apr 29 '11 at 13:30
  • @netbrain: there are never "players on the server". Even if you *were* to run a session on the server machine, you would usually run a "client" version- but the point here is if you can't trust the server at that point you're up the creek without a paddle ;) – KTF Apr 29 '11 at 13:56

1 Answers1

10

Welcome to the world of networking. :)

These are the classic difficulties with game networking programming. There's a lot of different techniques to overcome these issues.

This blog has great info on the subject and will probably help you out.

http://gafferongames.com/networking-for-game-programmers/

You may be specifically interested in this article:

http://www.gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking

Good luck!

KTF
  • 1,349
  • 2
  • 14
  • 21
  • I just checked out the comment that Brian Roach included to your question with a link to [Dealing with Latency in Networked Games](http://stackoverflow.com/questions/42515/dealing-with-latency-in-networked-games) and one of the answers given there had a link to an article on how the Source engine handles these issue. It is a great read! You should check out that question. – KTF Apr 29 '11 at 13:59
  • @All : thanks for leading me in right direction....what i was thinking can i send the client key input to the server and server in return send physics and all updated data to client and then client updates its drawing by using that data....is this will be the right way..?? – Tornado May 11 '11 at 13:13
  • Yes and know- the big difference nowadays (begging with Quake) was that the client would make "educated guesses" while waiting for the server to return the updates from the user input...there's interpolation and wonderful (read: complicated) things like that to make this who process happen smoothly. Anyway- this all in those links provided. I recommend you read up on those. – KTF May 12 '11 at 19:14