TicTacToeServer Class

As the TicTacToeServer receives each client connection, it creates an instance of inner-class Player (Fig. 28.11, lines 182–304) to process the client in a separate thread. These threads enable the clients to play the game independently. The first client to connect to the server is player X and the second is player O. Player X makes the first move. The server maintains the information about the board so it can determine if a player’s move is valid.

 1   // Fig. 28.11: TicTacToeServer.java  2   // Server side of client/server Tic-Tac-Toe program.  3   import java.awt.BorderLayout;  4   import java.net.ServerSocket;  5   import java.net.Socket;  6   import java.io.IOException;  7   import java.util.Formatter; ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.