30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1130 Networking: Streams-Based Sockets and Datagrams Chapter 22<br />

61 notify the server of a successful connection and send <strong>to</strong> the client the Char that the client<br />

will place on the board when making a move. If Run is executing for CPlayer "X", lines<br />

65–76 execute, causing CPlayer "X" <strong>to</strong> wait for a second player <strong>to</strong> connect. Lines 70–72<br />

define a While loop that suspends the CPlayer "X" Thread until the server signals that<br />

CPlayer "O" has connected. The server notifies the CPlayer of the connection by setting<br />

the CPlayer’s threadSuspended variable <strong>to</strong> False (Fig. 22.5, lines 81–85). When<br />

threadSuspended becomes False, CPlayer exits the While loop of lines 70–72.<br />

Method Run executes the While structure (lines 80–114), enabling the user <strong>to</strong> play<br />

the game. Each iteration of this structure waits for the client <strong>to</strong> send an Integer specifying<br />

where on the board <strong>to</strong> place the "X" or "O"—the CPlayer then places the mark on<br />

the board, if the specified mark location is valid (e.g., that location does not already contain<br />

a mark). Note that the While structure continues execution only if Boolean variable<br />

done is False. This variable is set <strong>to</strong> True by event handler FrmServer_Closing<br />

of class FrmServer, which is invoked when the server closes the connection.<br />

Line 83 of Fig. 22.6 begins a While that loops until Socket property Available<br />

indicates that there is information <strong>to</strong> receive from the Socket (or until the server disconnects<br />

from the client). If there is no information, the thread goes <strong>to</strong> sleep for one second.<br />

Upon awakening, the thread uses property Disconnected <strong>to</strong> check for whether server<br />

variable disconnect is True (lines 83–91). If the value is True, the Thread exits the<br />

method (thus terminating the Thread); otherwise, the Thread loops again. <strong>How</strong>ever, if<br />

property Available indicates that there is data <strong>to</strong> receive, the While loop of lines 83–<br />

91 terminates, enabling the information <strong>to</strong> be processed.<br />

This information contains an Integer representing the location in which the client<br />

wants <strong>to</strong> place a mark. Line 94 calls method ReadInt32 of the BinaryReader object<br />

(which reads from the NetworkStream created with the Socket) <strong>to</strong> read this<br />

Integer. Line 97 then passes the Integer <strong>to</strong> Server method ValidMove. If this<br />

method validates the move, the CPlayer places the mark in the desired location.<br />

Method ValidMove (Fig. 22.5, lines 95–139) sends <strong>to</strong> the client a message indicating<br />

whether the move was valid. Locations on the board correspond <strong>to</strong> numbers from 0–<br />

8 (0–2 for the first row, 3–5 for the second and 6–8 for the third). All statements in method<br />

ValidMove are enclosed in a SyncLock statement that allows only one move <strong>to</strong> be<br />

attempted at a time. This prevents two players from modifying the game’s state information<br />

simultaneously. If the CPlayer attempting <strong>to</strong> validate a move is not the current player<br />

(i.e., the one allowed <strong>to</strong> make a move), that CPlayer is placed in a wait state until it is that<br />

CPlayer’s turn <strong>to</strong> move. If the user attempts <strong>to</strong> place a mark on a location that already<br />

contains a mark, method ValidMove returns False. <strong>How</strong>ever, if the user has selected<br />

an unoccupied location (line 114), lines 117–121 place the mark on the local representation<br />

of the board. Line 127 notifies the other CPlayer that a move has been made, and line 130<br />

invokes the Pulse method so that the waiting CPlayer can validate a move. The method<br />

then returns True <strong>to</strong> indicate that the move is valid.<br />

When a FrmClient application (Fig. 22.7) executes, it creates a TextBox <strong>to</strong> display<br />

messages from the server and the Tic-Tac-Toe board representation. The board is created out<br />

of nine CSquare objects (Fig. 22.8) that contain Panels on which the user can click, indicating<br />

the position on the board in which <strong>to</strong> place a mark. The FrmClient’s construc<strong>to</strong>r<br />

(line 46–82) opens a connection <strong>to</strong> the server (line 72) and obtains a reference <strong>to</strong> the connection’s<br />

associated NetworkStream object from TcpClient (line 73). Lines 80–81 start a

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

Saved successfully!

Ooh no, something went wrong!