Chapter 22 Networking: Streams-Based Sockets and Datagrams 1125 (Web domain)
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