Transmission Control Protocol (TCP)

Reliable, connection-oriented transport: header, 3-way handshake, flags, and flow control.

Introduction: The Reliable Phone Call of the Internet

Imagine the internet-s underlying transport system as two types of postal services. The first is a fast, no-frills postcard service. You write a message, put an address on it, and drop it in the mailbox. It is incredibly efficient, but offers no guarantees. The postcard might get lost, arrive damaged, or even arrive after another postcard you sent later. This service is a lot like the User Datagram Protocol (UDP).

Now, imagine a second service: a formal, recorded phone call. Before you can exchange any information, you must first dial the number and wait for the other person to pick up and confirm they are ready to talk. During the conversation, you can ask them to repeat anything you did not hear clearly. You both speak in a structured manner, and you know the conversation is happening in the correct order. You formally say goodbye before hanging up. This is a perfect analogy for the Transmission Control Protocol (TCP).

TCP is one of the main protocols in the . Its fundamental role is to provide a reliable, ordered, and error-checked delivery of a stream of data between applications running on hosts communicating over an IP network. Where IP handles getting packets to the right computer, TCP makes sure the entire, complete message gets to the right application on that computer, flawlessly.

Core Characteristics of TCP

TCP achieves its mission by adhering to several key principles that define its behavior.

  • Connection-Oriented

    Before any application data can be sent, TCP must first establish a connection between the sender and the receiver. This is not a physical wire, but a logical, virtual circuit that exists for the duration of the conversation. This setup process, known as the three-way handshake, ensures both sides are ready to communicate and have synchronized their initial parameters. It makes the communication stateful, meaning both the client and server maintain information about the connection.

  • Reliable Delivery

    This is TCP-s defining feature. If data is lost or corrupted in transit, TCP will detect this and retransmit the affected data. This guarantee is achieved through a combination of mechanisms:

    • Sequence Numbers: TCP labels each byte of data it sends with a unique sequence number. Think of it as numbering every page in a book before mailing it.
    • Acknowledgments (ACKs): The receiver, upon getting data, sends back an acknowledgment message specifying the next sequence number it expects. This is like the recipient saying, I have received all pages up to page 35, now please send page 36.
    • Checksum: Each TCP segment includes a checksum value. Both the sender and receiver calculate this value. If the values do not match upon arrival, the receiver knows the data was corrupted and discards it, relying on the acknowledgment mechanism to trigger a retransmission.
  • Ordered Data Stream

    IP packets can arrive out of order. TCP uses the sequence numbers to reassemble the data stream in the correct sequence before passing it to the application. If the book pages arrive in the order 1, 2, 5, 4, 3, TCP will hold onto them and reorder them correctly as 1, 2, 3, 4, 5 before letting the application read the story. This ensures that a file transfer, a web page, or an email is reconstructed exactly as it was sent.

  • Full-Duplex Communication

    A TCP connection is full-duplex, meaning that data can be sent and received in both directions simultaneously over the same connection. Once the connection is established, both the client and the server can send data to each other at the same time, much like a natural telephone conversation.

Establishing a Connection: The Three-Way Handshake

The process of creating a reliable TCP connection is a carefully choreographed exchange called the three-way handshake. Its purpose is to ensure both sides are present, agree to communicate, and synchronize their initial sequence numbers.

Let us break down the steps from the perspective of a client (e.g., your browser) wanting to connect to a server (e.g., a web server):

  1. Step 1: The Client Sends a SYN Packet

    The client initiates the conversation by sending a special TCP segment to the server. This first message has the SYN (Synchronize) flag set to 1. This is the equivalent of calling someone and saying, Hello, I would like to start a conversation. Along with the SYN flag, the client sends its own randomly chosen , let us say SEQ=100.

  2. Step 2: The Server Responds with a SYN-ACK Packet

    If the server is active and the port is open, it receives the client's SYN packet. It now knows the client wants to connect. The server responds with a segment that has two flags set:

    • The SYN flag is set to 1, signifying that it too is ready and is sending its own ISN, let us say SEQ=300.
    • The ACK (Acknowledgment) flag is set to 1, to acknowledge the client-s request. To do this, it sets the Acknowledgment number to the client-s sequence number plus one (ACK=101).

    This is the server replying, Hello, I hear you and am ready to talk. I acknowledge your starting number 100, and my own starting number is 300.

  3. Step 3: The Client Acknowledges the Server-s Response

    Finally, the client receives the server-s SYN-ACK packet. The client now knows the server is ready. To complete the handshake, the client sends one last segment. This segment has the ACK flag set to 1, and its Acknowledgment number is set to the server-s sequence number plus one (ACK=301). This final packet does not require a SYN flag because the synchronization is already established.

    This is the client saying, Great, I acknowledge your starting number 300. Let-s begin exchanging data. At this point, the connection is considered established, and both client and server can begin sending application data.

Terminating a Connection: The Four-Way Handshake

Just as a connection must be carefully established, it must also be gracefully terminated to ensure no data is lost. Because TCP is a full-duplex protocol, each direction of the data flow must be shut down independently. This leads to a four-step process.

Let us assume the client decides to end the session:

  1. Step 1: The Client Sends a FIN Packet

    The client, having finished sending its data, sends a TCP segment with the FIN (Finish) flag set. This signals to the server: I am done sending data to you.

  2. Step 2: The Server Acknowledges the FIN

    The server receives the FIN packet and sends back an ACK segment to acknowledge it. At this point, the server knows the client will not send any more data, but the server itself might still have data to send. The connection is now in a half-closed state.

  3. Step 3: The Server Sends Its Own FIN Packet

    Once the server has also finished sending all of its data to the client, it sends its own FIN packet. This signals: I am also done sending data to you.

  4. Step 4: The Client Acknowledges the Server-s FIN

    The client receives the server-s FIN packet and sends a final ACK to acknowledge it. After sending this last ACK, the client waits for a short period (to handle any stray, delayed packets) before completely closing the connection. Once the server receives this final ACK, it also closes the connection. The session is now fully terminated.

Managing Data Flow: Flow Control and Congestion Control

TCP incorporates sophisticated mechanisms to manage the rate of data transmission, not just to ensure reliability, but also to prevent overwhelming the receiver and the network itself.

Flow Control: Protecting the Receiver

Flow control is a mechanism that prevents a fast sender from flooding a slow receiver with too much data. Every computer has a limited amount of memory, called a receive buffer, for each TCP connection to store incoming data before it is passed to the application.

The TCP header includes a 16-bit field called the Window Size. The receiver uses this field to advertise to the sender how much free buffer space it has available. If the receiver is busy and its buffer is nearly full, it will advertise a smaller window size. The sender is obliged to send no more data than the advertised window size before waiting for the next acknowledgment. This mechanism allows for an efficient and adaptive flow of data that matches the receiver-s processing capacity.

Congestion Control: Protecting the Network

While flow control protects the receiver, congestion control protects the network. The internet is a shared resource, and if many senders transmit data at their maximum rate simultaneously, intermediate routers can become overwhelmed, leading to network congestion, packet loss, and delays for everyone.

TCP uses packet loss as an implicit signal that the network is congested. It maintains an internal variable called the Congestion Window (cwnd), which limits how much data it can send at any given time. TCP starts by sending a small amount of data (slow start) and gradually increases its sending rate as long as acknowledgments are received promptly. If a packet is lost (indicated by a missing ACK), TCP assumes the network is congested and drastically reduces its sending rate. It then begins a more cautious phase of increasing the rate (congestion avoidance). This adaptive behavior makes TCP a responsible network citizen, as it dynamically adjusts its speed to the current conditions of the network path.

    Transmission Control Protocol (TCP) | Teleinf Edu