Java Reference
In-Depth Information
A Generic Client
The HttpClient class of Example 5-4 was a special-purpose client. Example 5-8
defines a class, GenericClient , that can serve as a client for a variety of text-based
services. When you run this program, it connects to the host and port you have
specified on the command line. From that point on, it simply sends the text you
type to the server and then outputs the text the server sends in response to the
console.
You can use GenericClient to download files from a web server by sending a
simple HTTP protocol GET command, as HttpClient does. For big files, however,
the server's output scrolls by too quickly for this to be useful. GenericClient is
more useful for text-based interactive protocols. The Post Office Protocol (POP) is
such a protocol. You can use GenericClient to preview any email you have wait-
ing for you at your ISP (or elsewhere). An interaction, using GenericClient , with a
POP server might look as follows (The lines in bold are those typed by the user):
oxymoron% java com.davidflanagan.examples.net.GenericClient mail.isp.net 110
Connected to mail.isp.net/208.99.99.251:110
+OK QUALCOMM Pop server derived from UCB (version 2.1.4-R3) at mail.isp.net
starting.
user djf
+OK Password required for djf.
pass notrealpassword
+OK djf has 3 message(s) (2861 octets).
retr 3
+OK 363 octets
Received: from obsidian.oreilly.com (obsidian.oreilly.com [207.144.66.251])
by mail.isp.net (8.8.5/8.8.5) with SMTP id RAA11654
for djf@isp.net; Wed, 21 Jun 2999 17:01:50 -0400 (EDT)
Date: Wed, 25 Jun 1997 17:01:50 -0400 (EDT)
Message-Id: <199706252101.RAA11654@mail.isp.net>
From: "Paula Ferguson" <pf@oreilly.com>
To: djf@isp.net
Subject: schedule!
Aren't you done with that topic yet?
.
dele 3
+OK Message 3 has been deleted.
quit
+OK Pop server at mail.isp.net signing off.
Connection closed by server.
oxymoron%
The GenericClient class is fairly similar in structure to the SimpleProxyServer
class shown in Example 5-6. Like SimpleProxyServer , GenericClient uses an
anonymous second thread. This thread transfers data from server to client in paral-
lel with the main thread, which transfers data from client to server. Note that the
thread that transfers data from the server to the client translates ā€œ\nā€ line termina-
tors from the server into whatever the local line terminator is. By using two
threads, user input and server output can occur asynchronously, which, in fact, it
does in some protocols. The only complication in GenericClient is that the two
threads must have different priorities, because with some Java implementations, a
thread can't write to the console while another thread of the same priority is
blocked waiting to read from the console.
Search WWH ::




Custom Search