Java Reference
In-Depth Information
LISTING 17.2
Continued
32: }
33: digit.close();
34: } catch (IOException e) {
35: System.out.println(“IO Error:” + e.getMessage());
36: }
37: }
38: }
When making a finger request, specify a username followed by an at sign (“@”) and a
hostname, the same format as an email address. One famous real-life example is
johnc@
idsoftware.com
, the finger address of id Software founder John Carmack. You can
request his
.plan
file by running the
Finger
application as follows:
java Finger johnc@idsoftware.com
If
johnc
has an account on the
idsoftware.com
finger server, the output of this program
is his
.plan
file and perhaps other information (though it appears he's no longer updat-
ing it). The server also lets you know whether a user can't be found.
Blues News includes addresses for other game designers who provide
.plan
updates
such as id Software developer Timothee Besset (
ttimo@idsoftware.com
).
The
Finger
application uses the
StringTokenizer
class to convert an address in
user
@
host
format into two
String
objects:
user
and
host
(lines 10-13).
The following socket activities are taking place:
Lines 19-20
—A new
Socket
is created using the hostname and port 79, the port
traditionally reserved for finger services, and a timeout of 20 seconds is set.
n
Line 21
—The socket is used to get an
OutputStream
, which feeds into a new
PrintStream
object.
n
Line 22
—The finger protocol requires that the username be sent through the
socket, followed by a carriage return ('\015') and linefeed ('\012'). This is handled
by calling the
print()
method of the new
PrintStream
.
n
Lines 23-24
—After the username has been sent, an input stream must be created
on the socket to receive input from the finger server. A
BufferedReader
stream,
in
, is created by combining several stream-creation expressions together. This
stream is well suited for finger input because it can read a line of text at a time.
n
Lines 26-32
—The program loops as lines are read from the buffered reader. The
end of output from the server causes
in.readLine()
to return
null
, ending the
loop.
n