3. The Opening and Closing of a Connection
3.1. Opening a Connection
A "three-way handshake" is the procedure used to establish a connection. It is normally initiated by one end of the connection and responded to by the other. It will still work if both sides simultaneously initiate the procedure. Experience has shown that this strategy of opening a connection reduces the probability of false connections to an acceptably low level.
The simplest form of the three-way handshake is illustrated in the diagram below. The time order is line by line from top to bottom with certain lines numbered for reference. User events are placed in brackets as in [OPEN]. An arrow (-->) represents the direction of flow of a packet and an ellipsis (...) indicates a packet in transit. Side A and side B are the two ends of the connection. An "XXX" indicates a packet which is lost or rejected. The contents of the packet are shown on the center of each line. The state of both connections is that caused by the departure or arrival of the packet represented on the line. The contents of the data portion of a packet are left out for clarity.
Side A Side B
1. CLOSED LISTEN
2. [OPEN request]
SYN-SENT -> <SN=0><CTL=SYN><MDL=n> ...
3. --> SYN-RECEIVED
... <SN=0><AN=1><CTL=SYN,ACK><MDL=m> <--
4. ESTABLISHED <--
--> <SN=1><AN=1><CTL=ACK><DATA> ...
5. --> ESTABLISHED
In line 2 above the user at side A has requested that a connection be opened. Side A then attempts to open a connection by sending a SYN packet to side B which is in the LISTEN state. It specifies its initial sequence number, here zero. It places in the LENGTH field of the header the largest number of data octets it can consume in any one packet (MDL). The MDL is normally positive. The action of sending this packet places A in the SYN-SENT state.
In line 3 side B has just received the SYN packet from A. This places B in the SYN-RECEIVED state. B now sends a SYN packet to A which acknowledges the SYN it just received from A. Note that the AN field indicates B is now expecting to hear SN=1, thus acknowledging the SYN packet from A which used SN=0. B also specifies in the LENGTH field the largest number of data octets it is prepared to consume.
Side A receives the SYN packet from B which acknowledges A's original SYN packet in line 4. This places A in the ESTABLISHED state. Side A can now be confident that B expects to receive more packets from A.
A is now free to send B the first DATA packet. In line 5 upon receipt of this packet side B is placed into the ESTABLISHED state. DATA cannot be sent until the sender is in the ESTABLISHED state. This is because the LENGTH field is used to specify the MDL when opening the connection.
3.2. Recovering from a Simultaneous Active OPEN
It is of course possible that both ends of a connection may choose to perform an active OPEN simultaneously. In this case neither end of the connection is in the LISTEN state, both send SYN packets. A reliable bidirectional protocol must recover from this situation. It should recover in such a manner that the connection is successfully initiated.
Side A Side B
1. CLOSED CLOSED
2. [OPEN request]
SYN-SENT --> <SN=0><CTL=SYN><MDL=n> ...
3. ... [OPEN request]
<SN=0><CTL=SYN><MDL=m> <-- SYN-SENT
4. --> SYN-RECEIVED
... <SN=0><AN=1><CTL=SYN,ACK><MDL=m> <--
5. (packet finally arrives)
SYN-RECEIVED <-- <SN=0><CTL=SYN><MDL=m>
--> <SN=0><AN=1><CTL=SYN,ACK><MDL=n> --> ESTABLISHED
... <SN=1><AN=1><CTL=ACK> <--
6. (packet finally arrives)
ESTABLISHED <-- <SN=0><AN=1><CTL=SYN,ACK><MDL=m>
--> <SN=1><AN=1><CTL=ACK> ...
During simultaneous connection both sides of the connection cycle from the CLOSED state through SYN-SENT to SYN-RECEIVED, and finally to ESTABLISHED.
3.3. Detecting a Half-Open Connection
Any computer may crash after a connection has been established. After recovering from the crash it may attempt to open a new connection. The other end must be able to detect this condition and treat it as an error.
Side A Side
1. ESTABLISHED ESTABLISHED
--> <SN=0><AN=1><CTL=ACK><DATA> ...
-->
(crashes)
2. XXX <SN=1><AN=1><CTL=ACK><DATA> <--
3. (attempts to open new connection )
--> <SN=0><CTL=SYN><MDL=m> -->
... <SN=0><AN=1><CTL=RST,ACK> <-- (abort)
CLOSED
4. <--
(connection refused)
CLOSED
3.4. Closing a Connection
Either side may choose to close an established connection. This is accomplished by sending a packet with the FIN control bit set. No data may appear in a FIN packet. The other end of the connection responds by shutting down its end of the connection and sending a FIN, ACK in response.
Side A Side B
1. ESTABLISHED ESTABLISHED
2. [CLOSE request from user]
FIN-WAIT --> <SN=0><AN=1><CTL=FIN> ...
3. --> LAST-ACK
... <SN=1><AN=1><CTL=FIN,ACK> <--
4. TIME-WAIT <--
--> <SN=1><AN=0><CTL=ACK> ...
5. --> CLOSED
6. (after 2*SRTT time passes)
CLOSED
In line 2 the user on side A of the fully opened connection has decided to close it down by issuing a CLOSE call. No more data will be accepted for sending. If data remains unsent a message "Warning: Unsent data remains." is communicated to the user. No more data will be received. A packet containing a FIN but no data is constructed and sent. Side A goes into the FIN-WAIT state.
Side B sees the FIN sent and immediately builds a FIN, ACK packet in response. It then goes into the LAST-ACK state. The FIN, ACK packet is received by side A and an answering ACK is immediately sent. Side A then goes to the TIME-WAIT state. In line 5 side B receives the final acknowledgment of its FIN, ACK packet and goes to the CLOSED state. In line 6 after waiting to be sure its last acknowledgment was received side A goes to the CLOSED state (SRTT is the Smoothed Round Trip Time and is defined in section 6.3.1).