Chapter 22 (Http web server) Networking: Streams-Based Sockets and Datagrams 1135
Chapter 22 Networking: Streams-Based Sockets and Datagrams 1135 150 // receive messages sent to client 151 while ( true ) 152 ProcessMessage( reader.ReadString() ); 153 } 154 catch ( EndOfStreamException ) 155 { 156 MessageBox.Show( “Server is down, game over”, “Error”, 157 MessageBoxButtons.OK, MessageBoxIcon.Error ); 158 } 159 160 } // end method Run 161 162 // process messages sent to client 163 public void ProcessMessage( string message ) 164 { 165 // if the move player sent to the server is valid 166 // update the display, set that square’s mark to be 167 // the mark of the current player and repaint the board 168 if ( message == “Valid move.” ) 169 { 170 displayTextBox.Text += 171 “Valid move, please wait.rn”; 172 currentSquare.Mark = myMark; 173 PaintSquares(); 174 } 175 176 // if the move is invalid, display that and it is now 177 // this player’s turn again 178 else if ( message == “Invalid move, try again” ) 179 { 180 displayTextBox.Text += message + “rn”; 181 myTurn = true; 182 } 183 184 // if opponent moved 185 else if ( message == “Opponent moved” ) 186 { 187 // find location of their move 188 int location = reader.ReadInt32(); 189 190 // set that square to have the opponents mark and 191 // repaint the board 192 board[ location / 3, location % 3 ].Mark = 193 ( myMark == ‘X’ ? ‘O’ : ‘X’ ); 194 PaintSquares(); 195 196 displayTextBox.Text += 197 “Opponent moved. Your turn.rn”; 198 199 // it is now this player’s turn 200 myTurn = true; 201 } 202 Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 4 of 7.) Fig. 22.6
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services