Understanding the TCP Protocol: Origin, Functionality, and Communication Process

(1) What kind of protocol is TCP?

       TCP (Transmission Control Protocol) is a connection-oriented, reliable, IP-based transport layer protocol. Its main purpose is to provide reliable end-to-end transmission for data.

(2) The origin of the TCP protocol?

      In the previous section, we learned about the UDP protocol and understood that the UDP protocol is very simple and easy to implement. However, its reliability is poor; once a data packet is sent, there is no way to know if the other side has received it. To solve this problem, the TCP protocol was born. Using the TCP protocol can provide network security. Because when transmitting data using the TCP protocol, each sent data packet requires confirmation. If any data packet is lost, the acknowledgment packet is not received, and the sender will know to retransmit this data packet. Thus, the TCP protocol ensures data security.

(3) TCP Three-way Handshake

TCP Protocol

          The above diagram suggests a TCP protocol three-way handshake connection. Where Seq represents the sequence number request, Ack represents the acknowledgment sequence number, and SYN and ACK are control bits.

        1.   The first handshake

          During the first handshake to establish a connection, the client sends a SYN message (Seq=x, SYN=1) to the server, the client enters the SYN_SENT state, waiting for server confirmation.

          2. The second handshake

         First, the server receives the client’s request and sends a confirmation message (Ack = x + 1) back to the client.

         Then, the server sends a SYN packet (seq=y) again to the client to establish a connection request; at this point, the server enters the SYN_RECV state.

        3.   The third handshake

          The third handshake occurs when the client receives the server’s reply (SYN+ACK message). At this point, the client sends an ACK to the server. Once this message is sent, the client and the server enter the ESTABLISHED state, completing the three-way handshake.

(4) TCP Four-way Disconnection

       In TCP communication, each communication ends with a connection termination. This process involves four data packets, marked with a FIN flag to indicate the termination of the connection.

           TCP disconnection requires four steps:

           a.       The client sends a TCP packet with the FIN and ACK flags set, telling the server the communication is complete.

           b.      The server receives the client’s data and sends back an ACK packet to respond to the client.

           c.      The server transmits its own FIN/ACK packet to the client again.

           d.     The client, upon manually receiving the server’s FIN/ACK packet, responds with an ACK packet. Then, the communication ends.

(5) Some might ask, why does establishing a connection only require three steps, while disconnection requires four?

         Answer: When establishing a connection between the client and the server, upon receiving the SYN data from the client, the ACK/SYN is sent together to the client. However, when disconnecting, upon receiving the client’s FIN data, it only means the data has been sent, and the client will not send more data, but the service is still connected. It only indicates that the client has no more data to send to the server, but it does not mean the server has no data left to send to the client. When all the server’s data has been sent, it will send a FIN/ACK data, requesting to disconnect.