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

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

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

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

time, even while a packet is being received. The event handler for the TextBox processes<br />

the event and sends the data <strong>to</strong> the server.<br />

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

In this section, we present our caps<strong>to</strong>ne networking example—the popular game Tic-Tac-<br />

Toe, implemented with stream sockets and client/server techniques. The program consists<br />

of a FrmServer application (Fig. 22.5) and two FrmClient applications (Fig. 22.7);<br />

FrmServer allows the FrmClients <strong>to</strong> connect <strong>to</strong> the server and play Tic-Tac-Toe. We<br />

depict the output in Fig. 22.7. When the server receives a client connection, lines 67–78 of<br />

Fig. 22.5 create an instance of class CPlayer <strong>to</strong> process the client in a separate thread of<br />

execution. This enables the server <strong>to</strong> handle requests from both clients. The server assigns<br />

value "X" <strong>to</strong> the first client that connects (player X makes the first move), then assigns value<br />

"O" <strong>to</strong> the second client. Throughout the game, the server maintains information regarding<br />

the status of the board so that the server can validate players’ requested moves.<br />

<strong>How</strong>ever, neither the server nor the client can establish whether a player has won the<br />

game—in this application, method GameOver (lines 166–170) always returns False.<br />

Exercise 22.7 asks the reader <strong>to</strong> implement functionality that enables the application <strong>to</strong> determine<br />

a winner. Each FrmClient maintains its own GUI version of the Tic-Tac-Toe<br />

board <strong>to</strong> display the game. The clients can place marks only in empty squares on the board.<br />

Class CSquare (Fig. 22.8) is used <strong>to</strong> define squares on the Tic-Tac-Toe board.<br />

1 ' Fig. 22.5: Server.vb<br />

2 ' Server maintains a Tic-Tac-Toe game for two client applications.<br />

3<br />

4 Imports System.Windows.Forms<br />

5 Imports System.Net.Sockets<br />

6 Imports System.Threading<br />

7<br />

8 Public Class FrmServer<br />

9 Inherits Form<br />

10<br />

11 ' TextBox for displaying results<br />

12 Friend WithEvents txtDisplay As TextBox<br />

13<br />

14 Private board As Char() ' Tic-Tac-Toe game board<br />

15<br />

16 Private players As CPlayer() ' player-client applications<br />

17 Private playerThreads As Thread() ' Threads that run clients<br />

18<br />

19 ' indicates current player ("X" or "O")<br />

20 Private currentPlayer As Integer<br />

21<br />

22 ' indicates whether server has disconnected<br />

23 Private disconnect As Boolean = False<br />

24<br />

25 Public Sub New()<br />

26 MyBase.New()<br />

27<br />

Fig. 22.5 Server side of client/server Tic-Tac-Toe program (part 1 of 4).

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

Saved successfully!

Ooh no, something went wrong!