The only blog you will need to understand working of TCP

Cloudastra Technologies
4 min readAug 10, 2023

Today we are going to take a deeper look at Transmission Control Protocol (TCP) which is a communications standard that enables application programs and computing devices to exchange messages over a network

TCP Handshake or connection process:

Step 1 — SYN or Synchronize: The process starts when a client wants to establish a connection with a server. The client sends a TCP packet with the SYN flag set to the server. This packet contains a random sequence number, which is used to synchronize the sequence numbers between the client and server.

Step 2 — SYN-ACK or Synchronize-Acknowledgment: When the server receives the SYN packet, it responds with a TCP packet that has both the SYN and ACK flags set. The ACK flag acknowledges the receipt of the client’s SYN packet, and the server also includes its own random sequence number in this packet.

Step 3 — ACK or Acknowledgment: After receiving the SYN-ACK packet, the client acknowledges it by sending a TCP packet with the ACK flag set. This packet confirms the receipt of the server’s SYN-ACK packet and completes the three-way handshake.

At this point, the TCP connection is established, and data can be exchanged between the client and server.

The termination of a TCP connection involves a four-way handshake to gracefully close the connection between a client and server. The process ensures that both ends are aware of the termination, and any remaining data in transit is properly handled.

Now we will look at TCP connection termination process step-by-step:

Step 1 — Client Sends FIN (Finish): When the client decides to close the connection, it sends a TCP packet with the FIN flag set to the server. The FIN flag indicates that the client has no more data to send and wants to terminate the connection. The client enters the “FIN_WAIT_1” state after sending the FIN packet.

Step 2 — Server Acknowledges the FIN (ACK Flag Set): Upon receiving the FIN packet from the client, the server sends an acknowledgment (ACK) packet back to the client. The ACK flag confirms the receipt of the FIN packet from the client. At this point, the server enters the “CLOSE_WAIT” state, indicating that it has received the client’s request to close the connection and is waiting for its own application to close the connection.

Step 3 — Server Sends FIN (FIN Flag Set): When the server’s application decides to close the connection, it sends a FIN packet to the client. The FIN flag indicates that the server has no more data to send and wants to terminate the connection. The server enters the “LAST_ACK” state after sending the FIN packet.

Step 4 — Client Acknowledges the Server’s FIN (ACK Flag Set): Upon receiving the FIN packet from the server, the client acknowledges it by sending an ACK packet back to the server. The client enters the “TIME_WAIT” state after sending the ACK packet. The TIME_WAIT state is a waiting period during which the client waits to ensure that the acknowledgment from the server’s FIN is received correctly. It also allows any delayed packets in transit to be processed before the connection is fully closed.

Server Receives ACK and Closes the Connection. Upon receiving the ACK packet from the client, the server enters the “CLOSED” state and closes the connection. The client remains in the TIME_WAIT state for a brief period before transitioning to the “CLOSED” state as well.

TCP ensures that both sides of the connection have acknowledged the termination request before fully closing the connection. The TIME_WAIT state on the client side helps prevent potential issues due to delayed or out-of-order packets, ensuring a clean termination process.

During the TCP connection termination process, both the client and server can still send and receive data until the connection is completely closed. Once the connection is closed, the socket and associated resources are released, making them available for reuse.

Would you like to explore content? Check out our blogs at https://www.cloudastra.co/blogs Get in touch with us for business inquiries, at https://www.cloudastra.co/contact

--

--