Chapter 15: Client-Server Programming in Java
Note: this page applies to the now-obsolete first edition of the book. For
the second edition, please see
http://www.corewebprogramming.com.
This chapter of
Core Web Programming (pages 794-863) covers four options for
network programming in Java: raw sockets (both clients and servers),
the URL class, RMI, and JDBC.
1.02 Version: This version uses Java 1.1 exclusively.
However, for backward compatibility with Netscape 2, Netscape 3,
the standard (unpatched) version of Netscape 4.01-4.05, and Internet Explorer
3, a 1.02 version is available
separately.
Downloading files: On Windows 95/NT and Unix, click on the
links with the right mouse button to save the referenced HTML document
to a new location. On any platform, you can also click on the links
with the SHIFT key held down. On Netscape, this lets you save the HTML
source for the referenced file to a new location. In Internet
Explorer, this puts the referenced file in a separate browser window
(without frames), where you can then save it.
Contents
-
NetworkClient.java. This is a basic template for using the
Socket class for network clients. Uses the
SocketUtil class.
-
SocketUtil.java. This utility class simplifies the creation
of buffered and unbuffered input/output streams associated with
a socket.
-
NetworkClientTest.java. A test case for the network client;
connects to a server on the designated host and port, issues a
fixed line, reads a 1-line reply, and exits.
Uses NetworkClient and
SocketUtil.
-
TokTest.java. Lets you interactively test StringTokenizer
by supplying the string to parse the and delimiters as
command-line arguments.
-
AddressVerifier.java. This network client connects to a
mail server and verifies an address by issuing an EXPN
request. Supply the potential email address on the command line.
Uses MailAddress,
NetworkClient, and
SocketUtil.
-
MailAddress.java. A helper class for the AddressVerifier
that parses the email address.
-
UriRetriever.java. Given a URI (the part of the URL after
the hostname) and a hostname, makes an explicit socket
connection to the HTTP server, retrieves the document, and
prints the result.
Uses NetworkClient and
SocketUtil.
-
UrlRetriever.java. Given a normal URL, makes an explicit socket
connection to the HTTP server, retrieves the document, and
prints the result.
Uses UriRetriever,
NetworkClient, and
SocketUtil.
-
UrlRetriever2.java. Given a normal URL, uses the builtin
URL class to retrieve the document and
print the result.
- Browser.java. Uses
JEditorPane to build a simple but real browser in less than 2 pages
of code. Requires Java2 (JDK 1.2) or
a version of Swing for 1.1 that uses the javax packages.
Uses ExitListener,
JIconButton,
WindowUtilities, and
home.gif.
-
UrlTest.java. Reads a URL from the command line, then prints
the various components (host, protocol, port, etc.)
-
NetworkServer.java. This is a basic template for using the
ServerSocket and Socket classes for
network servers. Uses the
SocketUtil class.
-
NetworkServerTest.java. A test of the NetworkServer;
prints the host of clients that connect to it, as well as
reading/printing a single line of data from the client.
Uses the NetworkServer and
SocketUtil classes.
-
EchoServer.java. An HTTP server that generates an HTML
document showing the HTTP request and headers sent to it.
Handles either
GET or POST.
Uses the NetworkServer and
SocketUtil classes.
-
ThreadedEchoServer.java. A multi-threaded version of
the EchoServer. Uses the
EchoServer, NetworkServer and
SocketUtil classes.
There are two examples here. In the first, a very simple RMI
application is set up that simply sends a string. In the second,
RMI is used to create a numeric integration server (assumedly for
less floating-point-capable clients).
These require Java 1.1 or an RMI compatibility
package for 1.02.
These require all of the following:
- Java 1.1 or a JDBC compatibility
package for 1.02.
- The FastForward JDBC driver from
Connect Software. You have to obtain
and install it for this code to work,
or get a different driver and
adapt the connection code accordingly.
- A compatible database to connect to.
The database used in the examples is not
available on the WWW.
Classes
-
JDBC/TestDB.java. A simple example to show the approach to
making database connections:
connects to a database called
"testDB", retrieves "col1", "col2", and "col3", and shows
results in an ASCII-formatted table.
-
JDBC/ShowTable.java. Demonstrates the use of meta data.
Lets the user enter a host name, database name, and table
name in textfields, then displays the resultant table
graphically. Uses the
LabeledTextField and
CloseableFrame classes.
-
JDBC/LabeledTextField.java. Interface element used in the
ShowTable example.
-
JDBC/CloseableFrame.java. Simple Frame variation from
Chapter 11, used in the ShowTable example.
-
JDBC/UpdateTable.java. Demonstrates the use of prepared
statements. Extends the ShowTable example by adding a button
that, when clicked, pops up an interface that lets you add
a row of data to the database. Uses the
UpdateFrame,
LabeledTextField and
CloseableFrame classes.
-
JDBC/UpdateFrame.java. Popup frame giving one textfield
for each column of data, letting the user update the database
by adding a new row of data.