Dedicated web hosting - 1138 Networking: Streams-Based Sockets and Datagrams Chapter 22
1138 Networking: Streams-Based Sockets and Datagrams Chapter 22 server output after (1.) server output after (2.) server output after (3.) server output after (4.) Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 7 of 7.) Fig. 22.6 Line 229 of Fig. 22.5 begins a whilethat loops until Socketproperty Available indicates that there is information to receive from the Socket(or until the server disconnects from the client). If there is no information, the thread goes to sleep for one second. Upon awakening, the thread uses property Disconnected to check for whether server variable disconnectis true. If the value is true, the Threadexits the method (thus terminating the Thread); otherwise, the Thread loops again. However, if property Availableindicates that there is data to receive, the whileloop of lines 229 235 terminates, enabling the information to be processed. This information contains an intrepresenting the location in which the client wants to place a mark. Line 238 calls method ReadInt32 of the BinaryReader object (which reads from the NetworkStreamcreated with the Socket) to read this int. Line 242 then passes the int to Server method ValidMove. If this method validates the move, the Playerplaces the mark in the desired location. Method ValidMove(lines 101 131) sends to the client a message indicating whether the move was valid. Locations on the board correspond to numbers from 0 8(0 2for the first row, 3 5for the second and 6 8for the third). All statements in method ValidMove are enclosed in a lockstatement that allows only one move to be attempted at a time. This prevents two players from modifying the game s state information simultaneously. If the Player attempting to validate a move is not the current player (i.e., the one allowed to make a move), that Playeris placed in a wait state until it is that Player s turn to move. If the user attempts to place a mark on a location that already contains a mark, method ValidMove returns false. However, if the user has selected an unoccupied location (line 111), lines 114 115 place the mark on the local representation of the board. Line 121 notifies the other Player that a move has been made, and line 124 invokes the Pulse method so that the waiting Player can validate a move. The method then returns true to indicate that the move is valid. When a Clientapplication (Fig. 22.6) executes, it creates a TextBoxto display messages from the server and the Tic-Tac-Toe board representation. The board is created out of nine Squareobjects (Fig. 22.7) that contain Panels on which the user can click, indicating the position on the board in which to place a mark. The Client s constructor (line 50 82) opens a connection to the server (line 73) and obtains a reference to the connection s associ
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services