Java Reference
In-Depth Information
Example 5•7: Who.java (continued)
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
((Frame)e.getSource()).dispose();
}
});
TextArea t = new TextArea(10, 80);
t.setFont(new Font("MonoSpaced", Font.PLAIN, 10));
f.add(t, "Center");
f.pack();
f.show();
// Find out who's logged on
Socket s = null;
PrintWriter out = null;
BufferedReader in = null;
try {
// Connect to port 79 (the standard finger port) on the host
// that the applet was loaded from.
String hostname = this.getCodeBase().getHost();
s = new Socket(hostname, 79);
// Set up the streams
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
// Send a blank line to the finger server, telling it that we want
// a listing of everyone logged on instead of information about an
// individual user.
out.print("\n");
out.flush();
// Send it now!
// Now read the server's response and display it in the textarea
// The server should send lines terminated with \n. The
// readLine() method will detect these lines, even when running
// on a Mac that terminates lines with \r
String line;
while((line = in.readLine()) != null) {
t.append(line);
t.append("\n");
}
// Update the window title to indicate we're finished
f.setTitle("Who's Logged On: " + hostname);
}
// If something goes wrong, we'll just display the exception message
catch (IOException e) {
t.append(e.toString());
f.setTitle("Who's Logged On: Error");
}
// And finally, don't forget to close the streams!
finally {
try { in.close(); out.close(); s.close(); } catch(Exception e) {}
}
// And enable the button again
who.setEnabled(true);
}
}
Search WWH ::




Custom Search