Java Reference
In-Depth Information
/* Finally, we can read and write on the socket. */
/* ... */
( void
void ) close ( sock );
}
In the first recipe, we'll see how to do the connect in essentially one line of Java (plus a bit of
error handling). We'll then cover error handling and transferring data over a socket. Next,
we'll take a quick look at a datagram or UDP client that implements most of the TFTP
(trivial file transfer protocol) that has been used for two decades to boot diskless worksta-
tions. We'll end with a program that connects interactively to a text-based server such as Tel-
net or email.
A common theme through most of these client examples is to use existing servers so that we
don't have to generate both the client and the server at the same time. With one exception, all
of these are services that exist on any standard Unix platform. If you can't find a Unix server
near you to try them on, let me suggest that you take an old PC, maybe one that's under-
powered for running the latest Microsoft software, and put up a free, open source Unix sys-
tem on it. My personal favorite is OpenBSD, and the market's overall favorite is Linux. Both
are readily available on CD-ROM or can be installed for free over the Internet, and offer all
the standard services used in the client examples, including the time servers and TFTP. Both
have free Java implementations available.
I also provide basic coverage of “web services” clients. The term “web services” has come to
mean “program-to-program communication using HTTP.” The two general categories are
SOAP-based and REST-based. REST services are very simple—you send an HTTP request
and get back a response in plain text, or JSON ( Chapter 19 ) or XML ( Chapter 20 ). SOAP re-
quires a detailed XML specification for all aspects of the interaction, and requires that the
entire interaction itself take place in fairly verbose XML. It does offer some services that
REST does not, but these are rather advanced topics that we won't get into. There is more in-
formation on the client-side connections in Java Network Programming (O'Reilly). I don't
cover the server-side APIs for building web services—JAX-RS and JAX-WS—because
these are covered in several O'Reilly books .
Search WWH ::




Custom Search