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;
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!
source share