The Linux Programming Interface

(nextflipdebug5) #1

1272 Chapter 61


The SYN segments exchanged in the first two steps of the three-way handshake
may contain information in the options field of the TCP header that is used to
determine various parameters for the connection. See [Stevens et al., 2004],
[Stevens, 1994], and [Wright & Stevens, 1995] for details.

The labels inside angle brackets (e.g., <LISTEN>) in Figure 61-5 indicate the states
of the TCPs on either side of the connection.
The SYN flag consumes a byte of the sequence-number space for the connec-
tion. This is necessary so that this flag can be acknowledged unambiguously, since
segments with this flag set may also contain data bytes. This is why we show the
acknowledgement of the SYN M segment as ACK M+1 in Figure 61-5.

Figure 61-5: Three-way handshake for TCP connection establishment

61.6.5 TCP Connection Termination


Closing a TCP connection normally occurs in the following manner:


  1. An application on one end of the connection performs a close(). (This is often,
    but not necessarily, the client.) We say that this application is performing an
    active close.

  2. Later, the application on the other end of the connection (the server) also per-
    forms a close(). This is termed a passive close.


Figure 61-6 shows the corresponding steps performed by the underlying TCPs (here,
we assume that it is the client that does the active close). These steps are as follows:


  1. The client performs an active close, which causes the client TCP to send a FIN
    to the server TCP.

  2. After receipt of the FIN, the server TCP responds with an ACK. Any subsequent
    attempt by the server to read() from the socket yields end-of-file (i.e., a 0 return).

  3. When the server later closes its end of the connection, the server TCP sends a
    FIN to the client TCP.

  4. The client TCP responds with an ACK to acknowledge the server’s FIN.


SYN M

SYN N, ACK M+1

Client Server

ACK N+1

connect()
(blocks)

(returns)

accept()
(blocks)

(returns)

listen() <LISTEN>

<SYN_RECV>

<ESTABLISHED>

<ESTABLISHED>

<SYN_SENT>
Free download pdf