27.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server

This section presents the popular game Tic-Tac-Toe implemented by using client/server techniques with stream sockets. The program consists of a TicTacToeServer application (Figs. 27.1327.14) that allows two TicTacToeClient applications (Figs. 27.1527.16) to connect to the server and play Tic-Tac-Toe. Sample outputs are shown in Fig. 27.17.

TicTacToeServer Class

As the TicTacToeServer receives each client connection, it creates an instance of innerclass Player (Fig. 27.13, 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 ...

Get Java How to Program (early objects), 9/e 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.