Java Reference
In-Depth Information
Call ServerSocket(Q)
Returns instance
Closed
Listening
Create
structure
Local port
Fill in
local port,
set state
Local port
Q
Local IP
Local IP
*
Remote port
Remote port
*
Remote IP
Remote IP
*
Figure 5.7: Server-side socket setup.
In the normal case, this happens quickly. However, the Internet is a best-effort network, and
either the client's initial message or the server's response can get lost. For this reason, the TCP
implementation retransmits handshake messages multiple times, at increasing intervals. If the
client TCP does not receive a response from the server after some time, it times out and gives
up. In this case the constructor throws an IOException . The connection timeout is generally
long, and thus it can take on the order of minutes for a Socket() constructor to fail. If the server
is not accepting connections—say, if there is no program associated with the given port at the
destination—the server-side TCP will send a rejection message instead of an acknowledgment,
and the constructor will throw an IOException almost immediately.
The sequence of events at the server side is rather different; we describe it in Figures 5.7,
5.8, and 5.9. The server first creates an instance of ServerSocket associated with its well-known
port (here, Q). The socket implementation creates an underlying socket structure for the new
ServerSocket instance, and fills in Q as the local port and the special wildcard address (“*”
in the figures) for the local IP address. (The server may also specify a local IP address in the
constructor, but typically it does not. In case the server host has more than one IP address,
not specifying the local address allows the socket to receive connections addressed to any of
the server host's addresses.) The state of the socket is set to “Listening”, indicating that it is
ready to accept incoming connection requests addressed to its port. This sequence is depicted
in Figure 5.7.
The server can now call the ServerSocket 's accept() method, which blocks until the TCP
opening handshake has been completed with some client and a new connection has been
established. We therefore focus in Figure 5.8 on the events that occur in the TCP implementation
when a client connection request arrives. Note that everything depicted in this figure happens
“under the covers,” in the TCP implementation.
Search WWH ::




Custom Search