Java Reference
In-Depth Information
LISTING 17.5
Continued
90: try {
91: FileReader file = new FileReader (userName + “.plan”);
92: BufferedReader buff = new BufferedReader(file);
93: boolean eof = false;
94:
95: pw.println(“\nUser name: “ + userName + “\n”);
96:
97: while (!eof) {
98: String line = buff.readLine();
99:
100: if (line == null)
101: eof = true;
102: else
103: pw.println(line);
104: }
105:
106: buff.close();
107: } catch (IOException e) {
108: pw.println(“User “ + userName + “ not found.”);
109: }
110: }
111:
112: public static void main(String[] arguments) {
113: NewFingerServer nio = new NewFingerServer();
114: }
115: }
The finger server requires one or more user
.plan
files stored in text files. These files
should have names that take the form
username
.plan
—for example,
linus.plan
,
lucy.plan
, and
franklin.plan
. Before running the server, create one or more plan files
in the same folder as
NewFingerServer.class
.
When you're done, run the finger server with no arguments:
java NewFingerServer
The application waits for incoming finger requests, creating a nonblocking server socket
channel and registering one kind of key for a selector to look for: connection events.
Inside a
while
loop that begins on line 25, the server calls the
Selector
object's
select()
method to see whether the selector has received any keys, which would occur
when a finger client makes a connection. When it has,
select()
returns the number of
keys, and the statements inside the loop are executed.
After the connection is made, a buffered reader is created to hold a request for a
.plan
file. The syntax for the command is simply the username of the
.plan
file being
requested.