9.1 Socket Basics
A socket is a connection between two hosts. It can perform seven
basic operations:Connect to a remote machineSend dataReceive dataClose a connectionBind to a portListen for incoming dataAccept connections from remote machines on the bound port
Java's Socket class, which is
used by both clients and servers, has methods that correspond to the
first four of these operations. The last three operations are needed
only by servers, which wait for clients to connect to them. They are
implemented by the ServerSocket class, which is
discussed in the next chapter. Java programs normally use client
sockets in the following fashion:The program creates a new
socket with a constructor.The socket attempts to connect to the remote host.Once the connection is established, the local and remote hosts get
input and output streams from the socket and use those streams to
send data to each other. This connection is
full-duplex; both hosts can send and receive
data simultaneously. What the data means depends on the protocol;
different commands are sent to an FTP server than to an HTTP server.
There will normally be some agreed-upon hand-shaking followed by the
transmission of data from one to the other.When the transmission of data is complete, one or both sides close
the connection. Some protocols, such as HTTP 1.0, require the
connection to be closed after each request is serviced. Others, such
as FTP, allow multiple requests to be processed in a single
connection.
• Table of Contents• Index• Reviews• Reader Reviews• Errata• AcademicJava Network Programming, 3rd EditionBy
Elliotte Rusty Harold Publisher: O'ReillyPub Date: October 2004ISBN: 0-596-00721-3Pages: 706
Thoroughly revised to cover all the 100+ significant updates
to Java Developers Kit (JDK) 1.5, Java Network
Programming is a complete introduction to
developing network programs (both applets and applications)
using Java, covering everything from networking fundamentals
to remote method invocation (RMI). It includes chapters on
TCP and UDP sockets, multicasting protocol and content
handlers, servlets, and the new I/O API. This is the
essential resource for any serious Java developer.