26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

998 Networking Chapter 17<br />

Method sendData sends the String object <strong>to</strong> the client, flushes the output buffer and<br />

appends the same String <strong>to</strong> the JTextArea in the server window.<br />

Notice that the Server receives a connection, processes the connection, closes the<br />

connection and waits for the next connection. A more likely scenario would be a Server<br />

that receives a connection, sets up that connection <strong>to</strong> be processed as a separate thread of<br />

execution, and waits for new connections. The separate threads that process existing connections<br />

can continue <strong>to</strong> execute while the Server concentrates on new connection<br />

requests.<br />

Like class Server, class Client’s (Fig. 17.5) construc<strong>to</strong>r creates the GUI of the<br />

application (a JTextField and a JTextArea). The Client object displays its output<br />

in a JTextArea. When the main method (line 175–188) executes, it creates an instance<br />

of class Client, specifies the window’s default close operation and calls method run-<br />

Client (defined at lines 63–90). In this example, you can execute the client from any<br />

computer on the Internet and specify the Internet address or host name of the server computer<br />

as a command-line argument <strong>to</strong> the program. For example,<br />

java Client 192.168.1.15<br />

connects <strong>to</strong> the Server on the computer with Internet address 192.168.1.15.<br />

1 // Fig. 17.5: Client.java<br />

2 // Set up a Client that will read information sent<br />

3 // from a Server and display the information.<br />

4<br />

5 // <strong>Java</strong> core packages<br />

6 import java.io.*;<br />

7 import java.net.*;<br />

8 import java.awt.*;<br />

9 import java.awt.event.*;<br />

10<br />

11 // <strong>Java</strong> extension packages<br />

12 import javax.swing.*;<br />

13<br />

14 public class Client extends JFrame {<br />

15 private JTextField enterField;<br />

16 private JTextArea displayArea;<br />

17 private ObjectOutputStream output;<br />

18 private ObjectInputStream input;<br />

19 private String message = "";<br />

20 private String chatServer;<br />

21 private Socket client;<br />

22<br />

23 // initialize chatServer and set up GUI<br />

24 public Client( String host )<br />

25 {<br />

26 super( "Client" );<br />

27<br />

28 // set server <strong>to</strong> which this client connects<br />

29 chatServer = host;<br />

Fig. Fig. 17.5 17.5 Demonstrating the client portion of a stream-socket connection between a<br />

client and a server (part 1 of 5).

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

Saved successfully!

Ooh no, something went wrong!