Java Reference
In-Depth Information
Chapter 13. Network Clients
Introduction
Java can be used to write many types of networked programs. In traditional socket-based
code, the programmer is responsible for structuring the interaction between the client and
server; the TCP “socket” code simply ensures that whatever data you send gets to the other
end. In higher-level types, such as RMI, CORBA, and EJB, the software takes over increas-
ing degrees of control. Sockets are often used for connecting to “legacy” servers; if you were
writing a new application from scratch, you'd be better off using a higher-level service.
It may be helpful to compare sockets with the telephone system. Telephones were originally
used for analog voice traffic, which is pretty unstructured. Then it began to be used for some
“layered” applications; the first widely popular one was facsimile transmission, or fax.
Where would fax be without the widespread availability of voice telephony? The second
wildly popular layered application historically was dial-up TCP/IP. This coexisted with the
Web to become popular as a mass-market service. Where would dial-up IP be without widely
deployed voice lines? And where would the Internet be without dial-up IP?
Sockets are layered like that too. The Web, RMI, JDBC, CORBA, and EJB are all layered on
top of sockets. HTTP is now the most common protocol, and should generally be used for
new applications when all you want is to get data from point b to point a.
Ever since the alpha release of Java (originally as a sideline to the HotJava browser) in May
1995, Java has been popular as a programming language for building network applications.
It's easy to see why, particularly if you've ever built a networked application in C. First, C
programmers have to worry about the platform they are on. Unix uses synchronous sockets,
which work rather like normal disk files vis-a-vis reading and writing, whereas Microsoft
OSes use asynchronous sockets, which use callbacks to notify when a read or write has com-
pleted. Java glosses over this distinction. Further, the amount of code needed to set up a
socket in C is intimidating. Just for fun, Example 13-1 shows the “typical” C code for setting
up a client socket. And remember, this is only the Unix part. And only the part that makes
and closes the connection. To be portable to Windows, it would need some additional condi-
tional code (using C's #ifdef mechanism). And C's #include mechanism requires that ex-
Search WWH ::




Custom Search