You are correct, the current example does not send or handle the information about actual actions on the server. At the moment, the game does not synchronize between the two clients connected to the server.
To handle actions on the server and communicate between clients, let's update the "Server" and "Client" classes to handle the synchronization.
- Update the
Server class:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private static final int PORT = 8989;
private static List<GameThread> clients = new ArrayList<>();
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server started");
while (true) {
Socket socket = server.accept();
System.out.println("Client connected");
GameThread gameThread = new GameThread(socket);
gameThread.start();
clients.add(gameThread);
}
} catch (IOException e) {
e.printStackTrace();
}
}
static void broadcastAction(String action) {
for (GameThread client : clients) {
client.sendAction(action);
}
}
static class GameThread extends Thread {
private Socket socket;
private ObjectInputStream input;
private ObjectOutputStream output;
public GameThread(Socket socket) {
this.socket = socket;
try {
output = new ObjectOutputStream(socket.getOutputStream());
input = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
String action = (String) input.readObject();
// Broadcast action from the client to all connected clients
broadcastAction(action);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void sendAction(String action) {
try {
output.writeObject(action);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
In the Server class, add a broadcastAction method and invoke it in the run method of GameThread. The broadcastAction method sends the action to all the connected clients.
- Update the
Client class:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class Client {
private String host;
private int port;
private Socket socket;
private ObjectOutputStream output;
private ObjectInputStream input;
private Game game;
public Client(String host, int port, Game game) {
this.host = host;
this.port = port;
this.game = game;
}
public void connect() {
try {
socket = new Socket(host, port);
output = new ObjectOutputStream(socket.getOutputStream());
input = new ObjectInputStream(socket.getInputStream());
System.out.println("Connected to the server");
new Thread(() -> {
try {
while (true) {
String action = (String) input.readObject();
game.handleAction(action);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendAction(String action) {
try {
output.writeObject(action);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the Client class, add a new thread in the connect method to read incoming actions from the server and handle them using a new method handleAction of the Game class.
- Update the
Game class:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Game extends Application {
private Client client;
private Label[][] board = new Label[3][3];
@Override
public void start(Stage primaryStage) {
/* ... previous code ... */
startButton.setOnAction(e -> {
client = new Client("localhost", 8989, this);
client.connect();
});
/* ... previous code ... */
}
public void handleAction(String action) {
if (action.startsWith("MOVE")) {
String[] parts = action.split(" ");
int row = Integer.parseInt(parts[1]);
int col = Integer.parseInt(parts[2]);
Label cell = board[row][col];
if (cell.getText().equals("-")) {
cell.setText("O");
}
}
}
/* ... previous code ... */
}
In the Game class, add a new method handleAction that updates the board with the "O" characters based on the received action from the server.
Now, run the game as described in the previous response. With the updated code, when the first player makes a move with the "X" character, the second player should see the move and vice versa. Keep in mind that this example still lacks proper turn handling, win condition checking, and other game features. To create a complete game, you should further improve the code and logic.