s all readers know, Java is practically a synonym for Internet programming. There are
a number of reasons for this, not the least of which is its ability to generate secure,
cross-platform, portable code. However, one of the most important reasons that Java
is the premier language for network programming are the classes defined in the java.net
package. They provide an easy-to-use means by which programmers of all skill levels can
access network resources.
This chapter explores the java.net package. It is important to emphasize that networking
is a very large and at times complicated topic. It is not possible for this topic to discuss all
of the capabilities contained in java.net. Instead, this chapter focuses on several of its core
classes and interfaces.
Before we begin, it will be useful to review some key networking concepts and terms. At the
core of Java's networking support is the concept of a socket. A socket identifies an endpoint
in a network. The socket paradigm was part of the 4.2BSD Berkeley UNIX release in the early
1980s. Because of this, the term Berkeley socket is also used. Sockets are at the foundation of
modern networking because a socket allows a single computer to serve many different clients
at once, as well as to serve many different types of information. This is accomplished through
the use of a port, which is a numbered socket on a particular machine. A server process is said
to "listen" to a port until a client connects to it. A server is allowed to accept multiple clients
connected to the same port number, although each session is unique. To manage multiple
client connections, a server process must be multithreaded or have some other means of
multiplexing the simultaneous I/O.
Socket communication takes place via a protocol. Internet Protocol (IP) is a low-level
routing protocol that breaks data into small packets and sends them to an address across a
network, which does not guarantee to deliver said packets to the destination. Transmission
Control Protocol (TCP) is a higher-level protocol that manages to robustly string together
these packets, sorting and retransmitting them as necessary to reliably transmit data. A
third protocol, User Datagram Protocol (UDP), sits next to TCP and can be used directly to
support fast, connectionless, unreliable transport of packets.
Once a connection has been established, a higher-level protocol ensues, which is
dependent on which port you are using. TCP/IP reserves the lower 1,024 ports for specific
Search WWH :