Tree-Way Handshake
The following scenario occurs when a TCP connection is established:
-
The server must be prepared to accept an incoming connection. This is normally done by calling
socket
,bind
, andlisten
and is called a passive open. -
The client issues an active open by calling connect. This causes the client TCP to send a “synchronize” (SYN) segment, which tells the server the client’s initial sequence number for the data that the client will send on the connection. Normally, there is no data sent with the SYN; it just contains an IP header, a TCP header, and possible TCP options (which we will talk about shortly).
-
The server must acknowledge (ACK) the client’s SYN and the server must also send its own SYN containing the initial sequence number for the data that the server will send on the connection. The server sends its SYN and ACK of the client’s SYN in a single segment.
-
The client must acknowledge the server’s SYN.
The minimum number of packets required for this exchange is three; hence, this is called TCP’s three-way handshake. We show the three segments in Figure 2.2.
We show the client’s initial sequence number as J and the server’s initial sequence number as K.The acknowledgment number in an ACK is the next expected sequence number for the end sending the ACK. Since a SYN occupies one byte of the sequence number space, the acknowledgment number in the ACK of each SYN is the initial sequence number plus one. Similarly, the ACK of each FIN is the sequence number of the FIN plus one.
TCP Connection Termination
While it takes three segments to establish a connection, it takes four to terminate a connection.
-
One application calls
close
first, and we say that this end performs the active close. This end’s TCP sends a FIN segment, which means it is finished sending data. -
The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an end-of file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection.
-
Sometime later, the application that received the end-of-file will
close
its socket. This causes its TCP to send a FIN. -
The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.
Since a Fin and an ACK are required in each direction, four segments are normally required. We use the qualifier “normally” because in some scenarios, the FIN in Step 1 is sent with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive close and could be combined into one segment. We show these packets in Figure 2.3.
A FIN occupies one byte of sequence number space just line a SYN. Therefore, the ACK of each FIN is the sequence number of the FIN plus one.
Between Steps 2 and 3 it is possible for data to flow from the end doing the passive close to the end doing the active close. This is called a half-close and we will talk about this in detail with the shutdown function in Section 6.6.
The sending of each FIN occurs when a socket is closed. We indicated that the application calls close
for this to happen, but realize that when a Unix process terminates, either voluntarily(calling exit
or having the main
function return) or involuntarily(receiving a signal that terminates the process), all open descriptors are closed, which will also cause a FIN to be sent on any TCP connection that is still open.
Although we show the client in Figure 2.3 performing the active close, either end–the client or the server–can perform the active close. Often the client performs the active close, but with some protocols (notably HTTP/1.0), the server performs the active close.
以上摘自 «Unix Network Programming Vol 1» 2.6节。