Client/Server UDP

I’m designing a game and I’d like to read more about how to keep the logic on the server, and let the client only display the game state.

That is, the client should only report to the server via udp packet what he is doing; that is what keys he’s pressing basicly. Server replies by sending the client new coordinates and stuff.

What is the best approach to do this?

what kind of game is it for?

Well, should that really matter?

It’s basicly a fighter game, each player controls a unit on the screen and that unit can move around and shoot at other players. Very simple.

There is a standalone server, that I will run, and the clients connect to it. So, the clients basicly need to notify the server what they’re doing, and the server needs to keep the clients informed of all changes in the game world.

I know how to send the packets to the server, and let the server reply. But that’s only with my prototype, where only a single client is connected. I need to know how to make the server handle multiple clients.

Note: this is for my 4k game :] so, libraries or any complex structures are not possible.

edit: I’m not look for someone to do things for me, all I’m looking for are references; tutorials, books, any texts explaining this. Just looking for pointers.

It matters because if it’s a slow game, ie turnbased, then it’s very simple and you can just send the whole state, and the clients doesn’t need to compute transitions between states. In an action game, the client will have to interpolate so you get smooth action even though game state data doesn’t arrive every frame…

It’s a pretty simple scenario. What I do is that on the server I make a unique key from each connected client’s remote address (ip & port). This key is put in a player class, that handles all interaction between the server and the player. The player classes are all put in a tree map, and when a packet arrives at the server the key is put together and the correct player is picked from the TreeMap. The incoming packet (in a ByteBuffer) is sent to the player and parsed. Put all keypresses that arrive in a queue or something, then in your game logic loop process each keypress to compute the new state.

Then send the new state to each player… this is a link describing how they update the state in quake 3. Found it interesting and useful, I do something similar. (Send dx from the last state successfully acked).

http://www.bookofhook.com/Article/GameDevelopment/TheQuake3NetworkingModel.html

If there are just a small number of entities you’ll be ok with sending the whole game state every snapshot (positions, velocity, what they’re doing)…

For actions requiring time to perform I’d send the starting time of the action, then the client can compute the following frames based on the starting time. (you don’t have time to send what actual animation frame is showing, every frame if you’re using a decent frame rate).

Use TCP instead of UDP. It’s less code and you don’t have to worry about lost packets and getting data in and out of buffers/arrays.

The easiest way to implement this is to use a simple lock step. The client would do the following:


send input to server
render  // rendering while waiting for the server will reduce lag.
read state from server

…and the server:


read input data from all clients
update state
send state to all clients

This is the way doom did it and will work fine on a lan. It will not work that well on the net since the game will run at speed of the slowest connection.

You can smooth things out by letting the client interpolate between the 2 previous states. But you’ll need to run at fixed rate and it’s more comlex. The animation will still pause if the slowest connection can’t keep up.

Type of game has to be considered! For an online flightsim like mine your approach definately is not an option.