TCP
Explore connection-oriented transport, the three-way handshake, segmentation, sequence ordering, overhead trade-offs, and protocols built on TCP.
Lesson goal
By the end of this lesson, you will understand how TCP establishes reliable connections via the three-way handshake, divides and reassembles data using sequence numbers, and balances reliability against latency and overhead.
TCP (Transmission Control Protocol) is a core rule standard on the internet that lets computers talk to each other. It breaks big data into small pieces, sends them safely, and puts them back together in the right order at the other end.
Key reliability functions of TCP include:
- Checks for errors: It tracks every piece and asks for missing or broken parts to be sent again (retransmission).
- Puts it in order: It sorts the pieces back into the correct sequence when they arrive.
The Three-Way Handshake
TCP uses a connection-oriented model. Before communication proceeds, TCP uses a three-way handshake as part of establishing and synchronizing the connection between client and server.
Client Server
| |
| ------------ Synchronize -----------> | (1. Client initiates connection)
| <-- Synchronize-Acknowledgment ------ | (2. Server acknowledges & synchronizes)
| ------------ Acknowledgment --------> | (3. Client acknowledges; connection established)
| |
| <====== Bidirectional Data Stream ===>|Why the handshake matters
The handshake provides the setup step for a TCP connection. It is the process that supports TCP’s connection-oriented approach, ensuring that communication begins through an established, synchronized connection rather than as unrelated exchanges.
How TCP Divides Data (Segmentation)
TCP does not treat all application data as one large block. Instead, it prepares large application data for transmission by dividing it into smaller units called segments. This process is known as segmentation.
These segments are carried as TCP data inside IP packets, as described in the Internet Protocol Suite.
How TCP Restores Order
Segments may need to be placed back into their original order before the application data can be reconstructed. TCP uses sequence numbers to identify each segment’s position in the original data:
Segment with sequence number 1
Segment with sequence number 2
Segment with sequence number 3TCP uses these sequence numbers to determine the correct order of the segments and reconstruct the original application data.
The process is:
- TCP divides large application data into smaller segments.
- TCP associates each segment with a sequence number.
- TCP uses the sequence numbers to restore the segments to their original order.
- TCP reconstructs the application data from the ordered segments.
Core idea
TCP segmentation breaks large application data into smaller segments, while sequence numbers allow TCP to restore those segments to the correct order at the destination.
TCP Overhead and Latency
TCP adds reliability features that require extra network activity. This activity creates overhead—additional work or communication—and can increase latency, the time involved in communication.
Where the overhead comes from
| TCP feature | Why it adds cost |
|---|---|
| Acknowledgements | TCP sends acknowledgements (ACKs) to confirm received data. These messages add network communication. |
| Ordering | TCP maintains the order of data. Supporting that ordering adds processing, memory buffering, and communication work. |
| Retransmission | TCP retransmits data when necessary. Sending the same data again adds traffic and delay. |
| Connection setup | TCP performs a 3-way handshake setup before communication. This introduces latency before data transfer can begin. |
These features support TCP’s communication behavior, but they are not free: each can contribute to additional network overhead or delay.
The central trade-off
TCP’s acknowledgements, ordering, and retransmission require extra handling during communication. Its connection setup adds work before communication begins. As a result, TCP can introduce both:
- Overhead during communication, from acknowledgements, ordering, and retransmission.
- Latency before communication, from connection setup.
Because of this overhead, TCP is slower and heavier than connectionless protocols. When applications require immediate, fast data transmission and can tolerate occasional packet loss (such as live streaming, voice calls, or multiplayer gaming), UDP is often the better choice.
Key trade-off
TCP prioritizes reliability and correctness over speed. If guaranteed delivery and ordering are mandatory, TCP is essential; if low latency and fast data delivery matter most, UDP is preferred.
Applications Built on TCP
TCP serves as the foundation for several common application protocols. Understanding these connections shows where TCP fits into application-level communication.
| Application protocol | Connection to TCP | Primary use case |
|---|---|---|
| HTTP / HTTPS | Built on TCP | Web browsing, REST APIs, web application communication |
| Email protocols (SMTP, IMAP, POP3) | Built on TCP | Reliable sending and receiving of emails |
| WebSockets | Built on TCP | Real-time, bidirectional interactive web connections |
| SSH / FTP | Built on TCP | Secure remote terminal access and file transfers |
The central relationship
TCP and an application protocol are related layers of communication. TCP provides the transport foundation, while protocols such as HTTP, email protocols, and WebSockets build applications on top of it.
Keep the layers distinct:
- TCP: the transport foundation providing reliable, ordered delivery.
- HTTP, email protocols, and WebSockets: application protocols connected to that foundation.
Takeaway
When studying HTTP, email protocols, or WebSockets, remember that they rely on TCP's connection management, error-checking, and sequence guarantees beneath the application layer.
Recap
- Connection-oriented: TCP uses a three-way handshake to establish a connection before communication begins.
- Segmentation & Ordering: TCP divides large data into segments, tracks them with sequence numbers, and restores the original sequence upon arrival.
- Reliability: Built-in error checking, acknowledgements, and retransmissions ensure that lost or corrupted packets are resent.
- Overhead & Latency: Handshake setup introduces latency before communication, while ACKs and retransmissions add overhead during transmission.
- TCP vs. UDP: Use TCP when data integrity and complete delivery are required; choose UDP when ultra-fast, low-latency transmission takes priority over reliability.
- Application Protocols: HTTP, email protocols (SMTP/IMAP), and WebSockets build directly on top of TCP.