Synchronization of game logic on server and client

I am creating a simple network game in Java where the player can move blocks using the keyboard keys. The game will run on the 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 it had problems with synchronization between the server and the client. Here is some code / description of what I need to better understand:

Main classes

 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(); } } 

Now the problem

 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); } } } 

I better understand how to handle Line A , Line B and Line C More precisely:

1 - What is a good way to update Thread game in a safe way?

2 - How to handle multiple commands? Can there be a queue?

2 - How can I make sure that the client and server will be synchronized?

I am new to programming online, so thanks for any help!

+4
source share
2 answers

Queues are definitely your friend when you program any server. If we assume a basic client-server model, each cycle of your game server should do something like the following:

  • Grab all the teams from the team queue.
  • Process teams.
  • Send the update to all connected clients, which will then apply them to their own queue, which will do the same.

In addition, clients must also have a queue of commands coming from the server that allow them to update their own state. With reasonable delays, the server and client should remain reasonably synchronized. A little de-sync will always take place, and there are different ways to fix it. (READ: client interpolation ).

For each cycle of the game, the game should basically free the queue of teams and apply them as desired.

+1
source

A possible solution for network synchronization: you can first synchronize all clients and the server with the same date / time using an NTP server (as if the delays were very different or there was a lot of jitter, you cannot achieve this yourself from the server). Then send your data in a frame in one second, related to the time when it should be executed, the client waits for the time specified in the frame and executes / updates the commands. There will always be a delay (possibly up to a few milliseconds per lan). Hope this helps.

0
source

Source: https://habr.com/ru/post/1499189/


All Articles