The Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte-stream based transport layer communication protocol, which operates in the transport layer of the OSI seven-layer model. It is a specialized transport protocol designed to provide a reliable end-to-end byte stream over unreliable interconnected networks.
seq=x
with the SYN=1
synchronization request flag, and enters the SYN_SENT state, waiting for server confirmation.ACK=1
and the synchronization request flag SYN=1
, along with its own sequence number seq=y
and the client's acknowledgment number ack=x+1
. At this point, the server enters the SYN_RECEIVED state.ACK=1
, its own sequence number seq=x+1
, and the server acknowledgment number ack=y+1
. Upon sending, the connection is confirmed to be in the ESTABLISHED state. The server, upon receiving the confirmation information, enters the ESTABLISHED state.FIN=1
, its own sequence number seq=u
, and enters the termination wait FIN-WAIT-1
state.ACK=1
confirmation flag and the client's acknowledgment number ack=u+1
, its own sequence number seq=v
, and then enters a close wait CLOSE-WAIT
state. After receiving this message, the client enters the termination wait FIN-WAIT-2
state.FIN=1
signal, confirmation flag ACK=1
, acknowledgment number ack=u+1
, its own sequence number seq=w
, and enters the last confirmation LAST-ACK
state.ACK=1
, acknowledgment number ack=w+1
, its own sequence number seq=u+1
, and enters the time wait TIME-WAIT
state. After the expiration of 2
maximum segment lifetimes, the client CLOSES
. The server immediately enters the CLOSE
state upon receiving the acknowledgment.The TCP
connection handshake is performed using sequence numbers, which are seq
. TCP
requires the seq
sequence number for reliable retransmission or reception and to avoid the inability to distinguish whether seq
is delayed or from an old connection when reusing a connection. Therefore, a three-way handshake is necessary to agree and determine the initial seq
sequence numbers of both parties, thus ensuring the completion of synchronized confirmation of sequence numbers.
If a two-way handshake were to occur, as follows, the client sends the initial sequence number seq=x
to the server. The server, upon receiving the message, sends the acknowledgment flag ACK=1
, acknowledgment number ack=x+1
, and its own sequence number req=y
. At this point, the server believes the connection is established, and upon receiving the message, the client also believes the request is established.
Considering the scenario of network data delay, if the client initiates a connection, and the packet becomes blocked in the network, and the client re-initiates the request. At this point, the packet changes the routing path and reaches the server. The server then believes the connection is established and sends the packet back to the client. After completing data communication, the connection is closed. Subsequently, the delayed packet from the client finally reaches the server. Because of the two-way handshake, the server believes the connection is established and sends an acknowledgment flag to the client. However, the client deems the sequence number invalid and does not establish the connection, leading to wastage of server resources as a connection that should not have been established is.
Back in class, the teacher gave the example of the red and blue army problem, also known as the two-army problem. From the perspective of communication reliability, there's no absolutely reliable protocol.
The two-army problem ultimately falls into a deadlock, indicating that an agreement can never be reached perfectly, and there's no completely reliable communication protocol. As a reliable transmission control protocol, TCP needs to ensure the reliable transmission of data while improving transmission efficiency. When the three-way handshake is completed, a very considerable level of reliability and transmission efficiency is already achieved. Continuing the handshake can indeed further improve the connection's reliability, but just like the curve of a logarithmic function, as the number of handshakes increases, the increase in reliability is not significant. In fact, in relation to the consumption of efficiency, multiple handshakes are detrimental to data transmission.
When establishing a connection, when the server is in the LISTEN
state and receives a connection request in the form of a SYN
packet, it sends an ACK
and SYN
together in one packet to the client. However, when closing a connection, when the server receives a FIN
packet from the other party, it means that the other party will no longer send data but can still receive it. At this point, the server may not have sent all of its data to the other party, so the server can either immediately close the connection or send some data to the other party first and then send a FIN
packet to indicate agreement to close the connection. Therefore, the server's ACK
and FIN
are generally sent separately, resulting in an additional step.
MSL
stands for Maximum Segment Lifetime
, and TCP allows different implementations to set different MSL
values.
First, to ensure that the last ACK
packet sent by the client can reach the server. Because this ACK
packet may be lost, from the server's perspective, it has sent the FIN+ACK
packet to request the disconnection, but the client has not responded, indicating that it may not have received the disconnection request packet sent by the server. As a result, the server will resend the disconnection request, and the client can receive this retransmitted packet within the 2MSL
time frame, then respond and restart the 2MSL
timer.
Second, to prevent the occurrence of invalid connection request packets in the current connection. After sending the last acknowledgment packet, the client can allow all segments generated in the time of this 2MSL
to disappear from the network. Thus, in the new connection, there won't be any request packets from the old connection.