24.8. Client/Server Tic-Tac-Toe Using a Multithreaded Server

In this section, we present the popular game Tic-Tac-Toe implemented by using client/server techniques with stream sockets. The program consists of a TicTacToeServer application (Figs. 24.1324.14) that allows two TicTacToeClient applications (Figs. 24.1524.16) to connect to the server and play Tic-Tac-Toe. Sample outputs are shown in Fig. 24.17.

Figure 24.13. Server side of client/server Tic-Tac-Toe program.
 1 // Fig. 24.13: TicTacToeServer.java
 2 // This class maintains a game of Tic-Tac-Toe for two clients.
 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;
 8 import java.util.Scanner; ...

Get Java™ How to Program, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.