Java Reference
In-Depth Information
Call accept()
Blocks until new
connection is established
Returns Socket instance
for this structure
Listening
Listening
Listening
Events of
Figure 5.8
Local port
Q
Local port
Q
Local port
Q
Local IP
*
Local IP
*
Local IP
*
Remote port
*
Remote port
*
Remote port
*
Remote IP
*
Remote IP
*
Remote IP
*
Established
Local port
Q
Local IP
W.X.Y.Z
Remote port
P
Remote IP
A.B.C.D
Figure 5.9: accept() processing.
Now we can consider (in Figure 5.9) what happens when the server program calls the
ServerSocket 's accept() method. The call unblocks as soon as there is something in its associ-
ated list of socket structures for new connections. (Note that this list may already be non-empty
when accept() is called.) At that time, one of the new connection structures is removed from
the list, and an instance of Socket is created for it and returned as the result of the accept() .
It is important to note that each structure in the ServerSocket 's associated list represents
a fully established TCP connection with a client at the other end. Indeed, the client can send
data as soon as it receives the second message of the opening handshake—which may be long
before the server calls accept() to get a Socket instance for it.
5.4.2 Closing a TCP Connection
TCP has a graceful close mechanism that allows applications to terminate a connection without
having to worry about loss of data that might still be in transit. The mechanism is also
designed to allow data transfers in each direction to be terminated independently, as in the
Search WWH ::




Custom Search