Archive for April, 2007

1134 Networking: Streams-Based Sockets and Datagrams Chapter 22 (Medical web site)

Tuesday, April 17th, 2007

1134 Networking: Streams-Based Sockets and Datagrams Chapter 22 98 protected void Client_Closing( 99 object sender, CancelEventArgs e ) 100 { 101 done = true; 102 } 103 104 // draws the mark of each square 105 public void PaintSquares() 106 { 107 Graphics g; 108 109 // draw the appropriate mark on each panel 110 for ( int row = 0; row < 3; row++ ) 111 for ( int column = 0; column < 3; column++ ) 112 { 113 // get the Graphics for each Panel 114 g = board[ row, column ].SquarePanel.CreateGraphics(); 115 116 // draw the appropriate letter on the panel 117 g.DrawString( board[ row, column ].Mark.ToString(), 118 this.Font, brush, 8, 8 ); 119 } 120 } // end method PaintSquares 121 122 // send location of the clicked square to server 123 protected void square_MouseUp( 124 object sender, System.Windows.Forms.MouseEventArgs e ) 125 { 126 // for each square check if that square was clicked 127 for ( int row = 0; row < 3; row++ ) 128 for ( int column = 0; column < 3; column++ ) 129 if ( board[ row, column ].SquarePanel == sender ) 130 { 131 CurrentSquare = board[ row, column ]; 132 133 // send the move to the server 134 SendClickedSquare( board[ row, column ].Location ); 135 } 136 } // end method square_MouseUp 137 138 // control thread that allows continuous update of the 139 // textbox display 140 public void Run() 141 { 142 // first get players's mark (X or O) 143 myMark = reader.ReadChar(); 144 idLabel.Text = "You are player "" + myMark + """; 145 myTurn = ( myMark == 'X' ? true : false ); 146 147 // process incoming messages 148 try 149 { Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 3 of 7.) Fig. 22.6
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Adelphia web hosting - Chapter 22 Networking: Streams-Based Sockets and Datagrams 1133

Monday, April 16th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1133 48 49 // default constructor 50 public Client() 51 { 52 InitializeComponent(); 53 54 board = new Square[ 3, 3 ]; 55 56 // create 9 Square objects and place them on the board 57 board[ 0, 0 ] = new Square( panel1, ‘ ‘, 0 ); 58 board[ 0, 1 ] = new Square( panel2, ‘ ‘, 1 ); 59 board[ 0, 2 ] = new Square( panel3, ‘ ‘, 2 ); 60 board[ 1, 0 ] = new Square( panel4, ‘ ‘, 3 ); 61 board[ 1, 1 ] = new Square( panel5, ‘ ‘, 4 ); 62 board[ 1, 2 ] = new Square( panel6, ‘ ‘, 5 ); 63 board[ 2, 0 ] = new Square( panel7, ‘ ‘, 6 ); 64 board[ 2, 1 ] = new Square( panel8, ‘ ‘, 7 ); 65 board[ 2, 2 ] = new Square( panel9, ‘ ‘, 8 ); 66 67 // create a SolidBrush for writing on the Squares 68 brush = new SolidBrush( Color.Black ); 69 70 // Make connection to sever and get the associated 71 // network stream. Start separate thread to allow this 72 // program to continually update its output in textbox. 73 connection = new TcpClient( “localhost”, 5000 ); 74 stream = connection.GetStream(); 75 76 writer = new BinaryWriter( stream ); 77 reader = new BinaryReader( stream ); 78 79 // start a new thread for sending and receiving messages 80 outputThread = new Thread( new ThreadStart( Run ) ); 81 outputThread.Start(); 82 } // end Client constructor 83 84 // Visual Studio .NET-generated code 85 86 [STAThread] 87 static void Main() 88 { 89 Application.Run( new Client() ); 90 } 91 92 protected void Client_Paint ( 93 object sender, System.Windows.Forms.PaintEventArgs e ) 94 { 95 PaintSquares(); 96 } 97 Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 2 of 7.) Fig. 22.6
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

1132 Networking: Streams-Based Sockets and Datagrams Chapter 22 (Web hosting unlimited bandwidth)

Monday, April 16th, 2007

1132 Networking: Streams-Based Sockets and Datagrams Chapter 22 board, if the specified mark location is valid (e.g., that location does not already contain a mark). Note that the whilestructure continues execution only if boolvariable doneis false. This variable is set to true by event handler Server_Closing of class Server, which is invoked when the server closes the connection. 1 // Fig. 22.6: Client.cs 2 // Client for the TicTacToe program. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 using System.Net.Sockets; 11 using System.Threading; 12 using System.IO; 13 14 // represents a tic-tac-toe player 15 public class Client : System.Windows.Forms.Form 16 { 17 private System.Windows.Forms.Label idLabel; 18 19 private System.Windows.Forms.TextBox displayTextBox; 20 21 private System.Windows.Forms.Panel panel1; 22 private System.Windows.Forms.Panel panel2; 23 private System.Windows.Forms.Panel panel3; 24 private System.Windows.Forms.Panel panel5; 25 private System.Windows.Forms.Panel panel6; 26 private System.Windows.Forms.Panel panel4; 27 private System.Windows.Forms.Panel panel7; 28 private System.Windows.Forms.Panel panel8; 29 private System.Windows.Forms.Panel panel9; 30 31 private Square[ , ] board; 32 private Square currentSquare; 33 34 private Thread outputThread; 35 36 private TcpClient connection; 37 private NetworkStream stream; 38 private BinaryWriter writer; 39 private BinaryReader reader; 40 41 private char myMark; 42 private bool myTurn; 43 44 private SolidBrush brush; 45 private System.ComponentModel.Container components = null; 46 47 bool done = false; Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 1 of 7.) Fig. 22.6
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1131 (Starting a web site)

Sunday, April 15th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1131 248 // signal the move is invalid 249 else 250 writer.Write( “Invalid move, try again” ); 251 252 // if game is over, set done to true to exit while loop 253 if ( server.GameOver() ) 254 done = true; 255 256 } // end while loop 257 258 // close the socket connection 259 writer.Close(); 260 reader.Close(); 261 socketStream.Close(); 262 connection.Close(); 263 264 } // end method Run 265 266 } // end class Player Fig. 22.5 Fig. 22.FiFig. 22.5g. 22.5Server side of client/server Tic-Tac-Toe program. (Part 6 of 6.) Fig. 22.5 Server(Fig. 22.5) uses its constructor (lines 35 48) to create a bytearray to store the moves the players have made (line 39). The program creates an array of two references to Player objects (line 41) and an array of two references to Thread objects (line 42). Each element in both arrays corresponds to a Tic-Tac-Toe player. Variable current- Playeris set to 0, which corresponds to player “X.”In our program, player “X”makes the first move (line 43). Lines 46 47 create and start ThreadgetPlayers, which the Server uses to accept connections so that the current Thread does not block while awaiting players. Thread getPlayers executes method SetUp (lines 65 92), which creates a TcpListenerobject to listen for requests on port 5000(lines 68 69). This object then listens for connection requests from the first and second players. Lines 72 73 and 79 80 instantiate Playerobjects representing the players, and lines 74 75 and 81 82 create two Threads that execute the Runmethods of each Playerobject. The Player constructor (Fig. 22.5, lines 165 181) receives as arguments a reference to the Socket object (i.e., the connection to the client), a reference to the Server object and an intindicating the mark (”X”or “O”) used by that player. In this case study, Server calls method Run(lines 193 264) after instantiating a Playerobject. Lines 198 206 notify the server of a successful connection and send to the client the charthat the client will place on the board when making a move. If Runis executing for Player”X”, lines 211 221 execute, causing Player”X” to wait for a second player to connect. Lines 217 218 define a whileloop that suspends the Player”X”Threaduntil the server signals that Player “O” has connected. The server notifies the Player of the connection by setting the Player s threadSuspendedvariable to false(line 89). When threadSuspended becomes false, Playerexits the whileloop of lines 217 218. Method Runexecutes the while structure (lines 226 256), enabling the user to play the game. Each iteration of this structure waits for the client to send an int specifying where on the board to place the “X” or “O” the Player then places the mark on the
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

1130 Networking: Streams-Based Sockets and Datagrams Chapter 22 (Ipower web hosting)

Sunday, April 15th, 2007

1130 Networking: Streams-Based Sockets and Datagrams Chapter 22 197 // display on the server that a connection was made 198 server.Display( “Player ” + ( number == 0 ? ‘X’ : ‘O’ ) 199 + ” connected” ); 200 201 // send the current player’s mark to the server 202 writer.Write( mark ); 203 204 // if number equals 0 then this player is X, so send 205 writer.Write( “Player ” + ( number == 0 ? 206 “X connectedrn” : “O connected, please waitrn” ) ); 207 208 // wait for another player to arrive 209 if ( mark == ‘X’ ) 210 { 211 writer.Write( “Waiting for another player” ); 212 213 // wait for notification from server that another 214 // player has connected 215 lock ( this ) 216 { 217 while ( threadSuspended ) 218 Monitor.Wait( this ); 219 } 220 221 writer.Write( “Other player connected. Your move” ); 222 223 } // end if 224 225 // play game 226 while ( !done ) 227 { 228 // wait for data to become available 229 while ( connection.Available == 0 ) 230 { 231 Thread.Sleep( 1000 ); 232 233 if ( server.disconnected ) 234 return; 235 } 236 237 // receive data 238 int location = reader.ReadInt32(); 239 240 // if the move is valid, display the move on the 241 // server and signal the move is valid 242 if ( server.ValidMove( location, number ) ) 243 { 244 server.Display( “loc: ” + location ); 245 writer.Write( “Valid move.” ); 246 } 247 Fig. 22.5 Fig. 22.FiFig. 22.5g. 22.5Server side of client/server Tic-Tac-Toe program. (Part 5 of 6.) Fig. 22.5
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Web design tools - Chapter 22 Networking: Streams-Based Sockets and Datagrams 1129

Saturday, April 14th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1129 145 // place code here to test for a winner of the game 146 return false; 147 } 148 149 } // end class Server 150 151 public class Player 152 { 153 internal Socket connection; 154 private NetworkStream socketStream; 155 private Server server; 156 private BinaryWriter writer; 157 private BinaryReader reader; 158 159 private int number; 160 private char mark; 161 internal bool threadSuspended = true; 162 163 // constructor requiring Socket, Server and int objects 164 // as arguments 165 public Player( Socket socket, Server serverValue, int newNumber ) 166 { 167 mark = ( newNumber == 0 ? ‘X’ : ‘O’ ); 168 169 connection = socket; 170 171 server = serverValue; 172 number = newNumber; 173 174 // create NetworkStream object for Socket 175 socketStream = new NetworkStream( connection ); 176 177 // create Streams for reading/writing bytes 178 writer = new BinaryWriter( socketStream ); 179 reader = new BinaryReader( socketStream ); 180 181 } // end constructor 182 183 // signal other player of move 184 public void OtherPlayerMoved( int location ) 185 { 186 // signal that opponent moved 187 writer.Write( “Opponent moved” ); 188 writer.Write( location ); // send location of move 189 } 190 191 // allows the players to make moves and receives moves 192 // from other player 193 public void Run() 194 { 195 bool done = false; 196 Fig. 22.5 Fig. 22.FiFig. 22.5g. 22.5Server side of client/server Tic-Tac-Toe program. (Part 4 of 6.) Fig. 22.5
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

Web design seattle - 1128 Networking: Streams-Based Sockets and Datagrams Chapter 22

Saturday, April 14th, 2007

1128 Networking: Streams-Based Sockets and Datagrams Chapter 22 93 94 // appends the argument to text in displayTextBox 95 public void Display( string message ) 96 { 97 displayTextBox.Text += message + “rn”; 98 } 99 100 // determine if a move is valid 101 public bool ValidMove( int location, int player ) 102 { 103 // prevent another thread from making a move 104 lock ( this ) 105 { 106 // while it is not the current player’s turn, wait 107 while ( player != currentPlayer ) 108 Monitor.Wait( this ); 109 110 // if the desired square is not occupied 111 if ( !IsOccupied( location ) ) 112 { 113 // set the board to contain the current player’s mark 114 board[ location ] = ( byte ) ( currentPlayer == 0 ? 115 ‘X’ : ‘O’ ); 116 117 // set the currentPlayer to be the other player 118 currentPlayer = ( currentPlayer + 1 ) % 2; 119 120 // notify the other player of the move 121 players[ currentPlayer ].OtherPlayerMoved( location ); 122 123 // alert the other player it’s time to move 124 Monitor.Pulse( this ); 125 126 return true; 127 } 128 else 129 return false; 130 } 131 } // end method ValidMove 132 133 // determines whether the specified square is occupied 134 public bool IsOccupied( int location ) 135 { 136 if ( board[ location ] == ‘X’ || board[ location ] == ‘O’ ) 137 return true; 138 else 139 return false; 140 } 141 142 // determines if the game is over 143 public bool GameOver() 144 { Fig. 22.5 Fig. 22.FiFig. 22.5g. 22.5Server side of client/server Tic-Tac-Toe program. (Part 3 of 6.) Fig. 22.5
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Virtual web hosting - Chapter 22 Networking: Streams-Based Sockets and Datagrams 1127

Friday, April 13th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1127 40 41 players = new Player[ 2 ]; 42 playerThreads = new Thread[ 2 ]; 43 currentPlayer = 0; 44 45 // accept connections on a different thread 46 getPlayers = new Thread( new ThreadStart( SetUp ) ); 47 getPlayers.Start(); 48 } 49 50 // Visual Studio .NET-generated code 51 52 [STAThread] 53 static void Main() 54 { 55 Application.Run( new Server() ); 56 } 57 58 protected void Server_Closing( 59 object sender, CancelEventArgs e ) 60 { 61 disconnected = true; 62 } 63 64 // accepts connections from 2 players 65 public void SetUp() 66 { 67 // set up Socket 68 listener = new TcpListener( 5000 ); 69 listener.Start(); 70 71 // accept first player and start a thread for him or her 72 players[ 0 ] = 73 new Player( listener.AcceptSocket(), this, 0 ); 74 playerThreads[ 0 ] = new Thread( 75 new ThreadStart( players[ 0 ].Run ) ); 76 playerThreads[ 0 ].Start(); 77 78 // accept second player and start a thread for him or her 79 players[ 1 ] = 80 new Player( listener.AcceptSocket(), this, 1 ); 81 playerThreads[ 1 ] = 82 new Thread( new ThreadStart( players[ 1 ].Run ) ); 83 playerThreads[ 1 ].Start(); 84 85 // let the first player know that the other player has 86 // connected 87 lock ( players[ 0 ] ) 88 { 89 players[ 0 ].threadSuspended = false; 90 Monitor.Pulse( players[ 0 ] ); 91 } 92 } // end method SetUp Fig. 22.5 Fig. 22.FiFig. 22.5g. 22.5Server side of client/server Tic-Tac-Toe program. (Part 2 of 6.) Fig. 22.5
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

1126 Networking: Streams-Based Sockets and Datagrams Chapter 22 (Web design service)

Friday, April 13th, 2007

1126 Networking: Streams-Based Sockets and Datagrams Chapter 22 Fig. 22.5 create an instance of class Player to process the client in a separate thread of execution. This enables the server to handle requests from both clients. The server assigns value “X” to the first client that connects (player X makes the first move), then assigns value “O” to the second client. Throughout the game, the server maintains information regarding the status of the board so that the server can validate players requested moves. However, neither the server nor the client can establish whether a player has won the game in this application, method GameOver (lines 143 147) always returns false. Exercise 22.7 asks the reader to implement functionality that enables the application to determine a winner. Each Client maintains its own GUI version of the Tic-Tac-Toe board to display the game. The clients can place marks only in empty squares on the board. Class Square(Fig. 22.7) is used to define squares on the Tic-Tac-Toe board. 1 // Fig. 22.5: Server.cs 2 // This class maintains a game of Tic-Tac-Toe for two 3 // client applications. 4 5 using System; 6 using System.Drawing; 7 using System.Collections; 8 using System.ComponentModel; 9 using System.Windows.Forms; 10 using System.Data; 11 using System.Net.Sockets; 12 using System.Threading; 13 using System.IO; 14 15 // awaits connections from two clients and allows them to 16 // play tic-tac-toe against each other 17 public class Server : System.Windows.Forms.Form 18 { 19 private System.Windows.Forms.TextBox displayTextBox; 20 21 private byte[] board; 22 23 private Player[] players; 24 private Thread[] playerThreads; 25 26 private TcpListener listener; 27 private int currentPlayer; 28 private Thread getPlayers; 29 30 private System.ComponentModel.Container components = null; 31 32 internal bool disconnected = false; 33 34 // default constructor 35 public Server() 36 { 37 InitializeComponent(); 38 39 board = new byte[ 9 ]; Fig. 22.5 Fig. 22.FiFig. 22.5g. 22.5Server side of client/server Tic-Tac-Toe program. (Part 1 of 6.) Fig. 22.5
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1125 (Web domain)

Thursday, April 12th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1125 Server method WaitForPackets (lines 52 72) executes an infinite loop while waiting for data to arrive at the Server. When information arrives, the UdpClient method Receive (line 57) receives a byte array from the client. We include Receive in the IPEndPoint object created in the constructor; this provides the method with a reference to an IPEndPoint into which the program copies the client s IP address and port number. This program will compile and run without an exception even if the reference to the IPEndPoint object is null, because method Receive initializes the IPEnd- Point if it is null. Good Programming Practice 22.1 Initialize all references to objects (to a value other than null). This protects code from methods that do not check their parameters for null references. Lines 60 65 update the Server s display to include the packet s information and content. Line 68 echoes the data back to the client, using UdpClient method Send. This version of Send takes three arguments: The byte array to send, an int representing the array s length and the IPEndPoint to which to send the data. We use array data returned by method Receive as the data, the length of array data as the length and the IPEndPoint passed to method Receive as the data s destination. The IP address and port number of the client that sent the data to Server are stored in receivePoint, so merely passing receivePointto Send allows Server to respond to the client. Class Client(Fig. 22.4) works similarly to class Server, except that the Client object sends packets only when the user types a message in a TextBox and presses the Enter key. When this occurs, the program calls event handler inputTextBox_KeyDown (lines 54 73). Lines 65 66 convert the string that the user entered in the TextBoxto a byte array. Line 69 calls UdpClient method Send to send the byte array to the Server that is located on localhost (i.e., the same machine). We specify the port as 5000, which we know to be Server s port. Line 32 instantiates a UdpClient object to receive packets at port 5001 we choose port 5001, because the Server already occupies port 5000. Method WaitFor- Packets of class Client (lines 76 90) uses an infinite loop to wait for these packets. The UdpClient method Receive blocks until a packet of data is received (line 81). The blocking performed by method Receive does not prevent class Client from performing other services (e.g., handling user input), because a separate thread runs method WaitForPackets. When a packet arrives, lines 84 87 display its contents in the TextBox. The user can type information into the Client window s TextBox and press the Enter key at any time, even while a packet is being received. The event handler for the TextBox processes the event and sends the data to the server. 22.6 Client/Server Tic-Tac-Toe Using a Multithreaded Server In this section, we present our capstone networking example the popular game Tic- Tac-Toe, implemented with stream sockets and client/server techniques. The program consists of a Server application (Fig. 22.5) and two Client applications (Fig. 22.6); Server allows the Clients to connect to the server and play Tic-Tac-Toe. We depict the output in Fig. 22.7. When the server receives a client connection, lines 72 83 of
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services