A Generic Client
Connect class of
Example 5-4 was a useful
first example of the
but it is too simple to use with most network protocols. Example 5-5 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
You can use
GenericClient to download files from a web
server by sending HTTP
commands, for example. (We’ll see what that protocol looks like in
Example 5-6.) For big files,
however, the server’s output scrolls by too quickly for this to be
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
waiting 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
java je3.net.GenericClient mail.isp.net 110Connected to mail.isp.net/188.8.131.52:110 +OK QUALCOMM Pop server derived from UCB (version 2.1.4-R3) at mail.isp.net starting.
USER david+OK Password required for david.
PASS notrealpassword+OK david has 3 message(s) (2861 octets).
RETR 3+OK 363 octets Received: from obsidian.oreilly.com (obsidian.oreilly.com [184.108.40.206]) by mail.isp.net ...