Reputation: 7905
I'm creating a simple Network game in Java, where the player can move Blocks around using the keyboard keys. The game will run on a local Server, and has a basic rule:
Each block will move automatically at 1 FPS rate, but the **user can send several move
commands** in this 1 second interval, thus updating the block position.
The code is almost complete, but í'm having trouble to synchronize things between server and client. Here's some code/description of what i need to understand better:
class Server{
ServerSocket server = new ServerSocket(port);
while (listening) {
Socket client = server.accept();
new Thread(new ClientHandler(client)).start();
}
}
class Game implements Runnable {
public void run() {
while (! gameOver)
tick();
}
}
class ClientHandler implements Runnable
{
Game game;
public ClientHandler(Socket client)
{
this.client = client;
//start game which runs at 1 FPS
long FPS = 1000L;
Timer timer = new Timer();
timer.schedule(new Game(FPS), 0L, FPS);
}
public void run()
{
/** Game is already running, needs to:
*
* 1 - Take any GameInput object from the user and update the game
* 2 - Send a GameState object to the user
* 3 - Client will receive it and render on screen,
* (hopefully in a synch state with the server)
*/
while ( ! game.gameOver)
{
//ObjectInputStream ois = ...;
// Line A
GameInput command = ois.readObject();
// Line B
//GameState state = game.update(command);
//ObjectOutputStream oos = ...;
// Line C
oos.writeObject(state);
}
}
}
What i need to understand better is how to handle Line A
, Line B
and Line C
.
To be more precise:
1 - What's a good way of update the game Thread in a safe way?
2 - How do i handle multiple commands? A Queue maybe?
2 - How can i ensure that client and server will be in synch?
I´m new to Networking programming, so thanks for any help!
Upvotes: 4
Views: 3021
Reputation: 596
A possible solution for the network synchronization : you can first synchronize all clients and server to same date/time using a NTP server (as if delays are very different, or if there a lot of jitter, you cannot achieve it by yourself from the server). Then send your data by frames of one second associated to the time when it has to be executed, client waits for the the time in indicated in the frame and execute/update the commands. There will always exits a latency , (possibly < to a few millisecond on a lan). Hope this helps.
Upvotes: 0
Reputation: 723
Queues are definitely your friend when you're programming any kind of server. If we assume a basic client-server model, each loop of your game server should be doing something like the following:
Likewise, the clients should also have a queue of commands coming from the server that allow them to update their own state. With reasonable latencies, the server and the client should stay reasonably synchronized. A little bit of de-synchronization will always be the case, and there are different ways to handle it. (READ: client-side interpolation).
For each loop of the game, the game should basically empty the queue of commands and apply them as desired.
Upvotes: 2