Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
6 min read
TCP Working: 3-Way Handshake & Reliable Communication

Every time we open a website, submit a form or sent an important data over the internet there is a massive process is going on behind the scene. The data we sent doesn’t simply goes here and there. As the Internet is massive and per second millions of data is going here and there, so, to manage this we need rules. And Engineers called these rules protocols.

So TCP (Transmission Control Protocol) is one of the important protocol to transmits data reliably and safely. TCP works on transport layer in OSI model. We know everything is created to solve some problems. So let’s take a look at TCP

TCP

TCP stands for Transmission Control Protocol. TCP works at transport layer in OSI model. TCP is very reliable and safe for sending data on Internet. The characteristics of TCP are:

  1. Reliability: TCP ensures that data reaches the correct destination. Each packet contains information that allows the receiver to verify the sender and check whether the data is received correctly. At the starting of TCP connection before sending data it does 3-way handshake to establishes a connection to check whether the connection is reliable or not.

  2. Ordering: Data on the Internet is broken into many packets. TCP ensures that these packets are reassembled in the correct order at the receiver before the data is delivered to the application.

  3. Loss detection:Loss Detection & Retransmission
    TCP checks whether all packets have arrived.

    • If all data is received correctly, the receiver sends an acknowledgment (ACK)

    • If some packets are missing, TCP automatically retransmits the lost data

This guarantees complete delivery.

  1. Flow Control: TCP controls the speed of data transfer between sender and receiver.
    If the receiver is slower, TCP reduces the sending rate so the receiver is not overwhelmed.

  2. Congestion control: TCP also adjusts the transmission rate based on network conditions.
    If the network is congested, TCP slows down data transmission to prevent overload.

Now you might have a question that what is 3-way handshake then let’s take a look at this—

TCP 3-Way Handshake

To understand TCP 3-way handshake we need to understand how TCP send data packets. Before sending data packets TCP creates a TCP segment which contains a header and data section. Along with data the header section is the most important part that carries vital information that supports 3-way handshake and subsequent data flow.

Header Contains source port address, destination port address, sequence number, acknowledgement number, header length, control flags, window size, check sum, urgent pointer. In these things the control flag Includes key indicators like SYN, ACK, FIN, and RST that control the TCP handshake process and connection termination.

Steps of 3-way handshake:

The TCP 3-way handshake is like a polite introduction between a client and a server before they start talking. It happens in three simple steps:

  1. Client → Server (SYN)

    The client says: “Hey server, I’d like to talk!” by sending a SYN (synchronize) packet. This also includes the client’s starting point for numbering the data it will send.

  2. Server → Client (SYN + ACK)

    The server replies: “Sure! I got your message, and I’m ready too.” by sending back a packet with SYN + ACK flags. This both acknowledges the client’s request and shares the server’s own starting sequence number.

  3. Client → Server (ACK)

    The client responds: “Great, I got your confirmation. Let’s start!” with an ACK. At this point, both sides are synchronized, and the connection is ready for data to flow safely.

This handshake makes sure both the client and server are on the same page before any actual data is sent, keeping communication reliable and orderly.

How TCP reassemble data in correct order

As we have learned, the TCP header contains an important field called the sequence number, which helps solve the problem of data reordering.

  • Sequence Numbers:
    A TCP sequence number is used to identify the position of data in a continuous stream. This allows the receiver to arrange incoming data in the correct order, even if it arrives out of sequence.

  • Starting Point (ISN):
    When a TCP connection is established, each side chooses a random Initial Sequence Number (ISN). This acts as the starting point for numbering the data bytes.

  • Tracking Segments:
    As data is sent in smaller pieces called segments, the sequence number indicates where each segment belongs in the overall data stream.

  • Counting Bytes:
    After a segment is sent, the sequence number moves forward by the number of bytes transmitted. This keeps both the sender and receiver synchronized.

  • Acknowledgements (ACKs):
    The receiver confirms successful delivery by sending an ACK number, which represents the next byte it expects to receive. This tells the sender that the data has been received correctly and in order.

How TCP handles packet loss and retransmission

While data is traveling across the internet, some packets may get lost or delayed due to network congestion or other issues. TCP is designed to handle this automatically.

  1. When TCP sends data, it expects an acknowledgement (ACK) from the receiver. This ACK tells the sender that the data has been received successfully.

  2. If the sender does not receive an ACK within a certain time, TCP assumes the packet was lost. In response, the sender retransmits the missing data.

  3. Because TCP uses sequence numbers, it knows exactly which part of the data is missing and resends only that portion. This prevents data loss and avoids unnecessary retransmissions.

Through this process of acknowledgements and retransmissions, TCP ensures that all data eventually reaches the receiver correctly, even on unreliable networks.

TCP Connection Termination Using FIN and ACK

Just like TCP carefully sets up a connection, it also closes the connection gracefully to make sure no data is lost.

  1. When one side has finished sending data, it sends a FIN (Finish) message. This means: “I’m done sending data, but I can still receive if needed.”

  2. The other side replies with an ACK, confirming that it received the FIN message.

  3. When the second side is also finished, it sends its own FIN message.
    Finally, the first side responds with an ACK, confirming the closure.

After this exchange, both sides agree that all data has been sent and received, and the TCP connection is safely closed.

This process ensures that:

  • All remaining data is delivered

  • Both sides know the communication is complete

  • No information is lost during shutdown

Summary

TCP ensures reliable, ordered, and safe communication. It starts with a 3-way handshake, uses sequence numbers and ACKs to track data, retransmits lost packets, and closes connections gracefully with FIN and ACK.

Thank you for reading & Happy Coding👨‍💻


References

  • ChaiCode Web Dev Cohort 2026

  • Cloudflare - TCP & Networking