Networked Game High-level Structure
An often-asked question is how to structure a networked game at the high level: what do you do in the client and in the server, and what do you put into the packets you send?
The basic client loop for this kind of game consists of:
Here is the basic loop for the server:
Here's what the functions do:
Read messages from clients, forwarding them to the appropriate objects. Send as much of you can of each clients outgoing queue to each client (to manage bandwidth). Handle log-in and log-out.
Given the current state of the objects, step one step forward (typically, around 20-50 milliseconds per step), doing collision detection, force and velocity integration, setting off triggered scripting events, etc.
Figure out what objects changed the most compared to what the client knows about, and queue data for those objects to the clients. Also, send "new object" messages for any new objects that may have entered the world.
Read the server connection, and dispatch any messages you receive to the appropriate objects.
Same thing as on the server, except you probably don't want to run the scripts etc; just trust that the scripts will run server-side and send appropriate object state changes to the clients.
Traverse the scene from the point of view of the player camera, rendering stuff to the screen.
Read keyboard/gamepad/whatever. Deal with OS events as necessary.
Package up the data you read from the user, and forward it to the user object on the server.
With this basic implementation, the object you control won't start moving on the screen until the user keypress made it to the server and back. Often, that's quite good enough.
I'd suggest you start with the scheme I describe above first, and then improve from there if necessary.
More pointers can be found in the Networking Forum FAQ on GameDev.net.