23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

}<br />

}<br />

}<br />

String response = "Hello " + s.getInetAddress() + " on port "<br />

+ s.getPort() + "\r\n";<br />

response += "This is " + s.getLocalAddress() + " on port "<br />

+ s.getLocalPort() + "\r\n";<br />

OutputStream out = s.getOutputStream();<br />

out.write(response.getBytes());<br />

out.flush();<br />

s.close();<br />

}<br />

catch (<strong>IO</strong>Exception e) {}<br />

}<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

<strong>Java</strong> I/O<br />

Here's some output from our server. The server is running on utopia.poly.edu. The client is<br />

connecting from titan.oit.unc.edu. Note how the port from which the connection comes<br />

changes each time; like most client programs, the telnet program picks a random local port for<br />

outgoing connections:<br />

% telnet utopia.poly.edu 2545<br />

Trying 128.238.3.21...<br />

Connected to utopia.poly.edu.<br />

Escape character is '^]'.<br />

Hello titan.oit.unc.edu/152.2.22.14 on port 50361<br />

This is utopia.poly.edu/128.238.3.21 on port 2545<br />

Connection closed by foreign host.<br />

% telnet utopia.poly.edu 2545<br />

Trying 128.238.3.21...<br />

Connected to utopia.poly.edu.<br />

Escape character is '^]'.<br />

Hello titan.oit.unc.edu/152.2.22.14 on port 50362<br />

This is utopia.poly.edu/128.238.3.21 on port 2545<br />

Connection closed by foreign host.<br />

5.5 URLViewer<br />

Example 5.6 is an improved version of the URLViewer you first encountered in Chapter 2.<br />

This is a simple application that provides a window in which you can view the contents of a<br />

URL. It assumes that those contents are more or less ASCII text. (In future chapters, I'll<br />

remove that restriction.) Figure 5.1 shows the result. Our application has a text area in which<br />

the user can type a URL, a Load button that the user uses to load the specified URL, and a<br />

StreamedTextArea component that displays the text from the URL. Each of these<br />

corresponds to a field in the URLViewer class.<br />

71

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

Saved successfully!

Ooh no, something went wrong!