26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 Networking 1011<br />

146<br />

147 application.setDefaultCloseOperation(<br />

148 JFrame.EXIT_ON_CLOSE );<br />

149<br />

150 application.waitForPackets();<br />

151 }<br />

152<br />

153 } // end class Client<br />

The Client window after sending a<br />

packet <strong>to</strong> the server and receiving<br />

the packet back from the server<br />

Fig. Fig. 17.7 17.7 Demonstrating the client side of connectionless client/server computing<br />

with datagrams (part 4 of 4).<br />

Notice that the DatagramSocket construc<strong>to</strong>r call (line 84) in this application does<br />

not specify any arguments. This construc<strong>to</strong>r allows the computer <strong>to</strong> select the next available<br />

port number for the DatagramSocket. The client does not need a specific port number,<br />

because the server receives the client’s port number as part of each DatagramPacket<br />

sent by the client. Thus, the server can send packets back <strong>to</strong> the same computer and port<br />

number from which the server receives a packet of information.<br />

Client method waitForPackets (lines 97–125) uses an infinite loop <strong>to</strong> wait for<br />

packets from the server. Line 111 blocks until a packet arrives. Note that this does not prevent<br />

the user from sending a packet, because the GUI events are handled in the event dispatch<br />

thread. It only prevents the while loop from continuing until a packet arrives at the<br />

Client. When a packet arrives, line 111 s<strong>to</strong>res the packet in receivePacket, and line<br />

114 calls method displayPacket (128–140) <strong>to</strong> display the packet’s contents in the<br />

JTextArea.<br />

17.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server<br />

In this section, we present the popular game Tic-Tac-Toe implemented by using client/server<br />

techniques with stream sockets. The program consists of a TicTacToeServer application<br />

(Fig. 17.8) that allows two TicTacToeClient applets (Fig. 17.9) <strong>to</strong> connect <strong>to</strong> the server<br />

and play Tic-Tac-Toe (outputs shown in Fig. 17.10). As the server receives each client connection,<br />

it creates an instance of inner class Player (lines 158–279 of Fig. 17.8) <strong>to</strong> process<br />

the client in a separate thread. These threads enable the clients <strong>to</strong> play the game independently.<br />

The server assigns Xs <strong>to</strong> the first client <strong>to</strong> connect (X makes the first move) and assigns

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!