%%{init: {'theme': 'base', 'themeVariables': {'primaryColor':'#2C3E50','primaryTextColor':'#fff','primaryBorderColor':'#16A085','lineColor':'#16A085','secondaryColor':'#E67E22','tertiaryColor':'#ecf0f1','background':'#ffffff','mainBkg':'#2C3E50','secondBkg':'#16A085','tertiaryBorderColor':'#95a5a6','clusterBkg':'#ecf0f1','clusterBorder':'#95a5a6','titleColor':'#2C3E50','edgeLabelBackground':'#ffffff','nodeTextColor':'#2C3E50'}}}%%
sequenceDiagram
participant C as Client
participant S as Server
Note over C,S: 1. Connection Establishment
C->>S: SYN (seq=100)
S->>C: SYN-ACK (seq=200, ack=101)
C->>S: ACK (ack=201)
Note over C,S: Connection OPEN
Note over C,S: 2. Reliable Data Transfer
C->>S: Data (seq=101, len=50)
S->>C: ACK (ack=151)
Note over C,S: ACK confirms receipt
S->>C: Data (seq=201, len=100)
C->>S: ACK (ack=301)
Note over C,S: 3. Connection Termination
C->>S: FIN (seq=151)
S->>C: ACK (ack=152)
S->>C: FIN (seq=301)
C->>S: ACK (ack=302)
Note over C,S: Connection CLOSED
731 Transmission Control Protocol (TCP)
731.1 Transmission Control Protocol (TCP)
TCP is a connection-oriented protocol that guarantees establishment of connection before data transmission and ensures reliable, ordered delivery.
731.1.1 TCP Characteristics
731.2 TCP Properties
Connection-Oriented: - 3-way handshake: SYN, SYN-ACK, ACK before data transfer - Connection state: Both endpoints maintain connection information - 4-way termination: Graceful connection closure
Reliable: - Acknowledgments: Receiver confirms receipt of each segment - Retransmission: Lost packets automatically resent - Ordering: Sequence numbers ensure correct order - Error Detection: Checksum for data integrity
Flow Control: - Sliding window: Prevents sender from overwhelming receiver - Window size: Receiver advertises how much data it can accept
Congestion Control: - Slow start: Gradually increases transmission rate - Congestion avoidance: Backs off when network congested - Fast retransmit/recovery: Quickly recovers from packet loss
Use Cases: - Firmware updates (must be reliable) - Configuration data - File transfers - HTTP (web browsing) - MQTT (reliable pub/sub messaging)
731.2.1 TCP Header Structure
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor':'#2C3E50','primaryTextColor':'#fff','primaryBorderColor':'#16A085','lineColor':'#16A085','secondaryColor':'#E67E22','tertiaryColor':'#ecf0f1','background':'#ffffff','mainBkg':'#2C3E50','secondBkg':'#16A085','tertiaryBorderColor':'#95a5a6','clusterBkg':'#ecf0f1','clusterBorder':'#95a5a6','titleColor':'#2C3E50','edgeLabelBackground':'#ffffff','nodeTextColor':'#2C3E50'}}}%%
graph TB
subgraph "TCP Segment"
subgraph "TCP Header: 20-60 bytes"
H1[Source Port<br/>16 bits]
H2[Dest Port<br/>16 bits]
H3[Sequence Number<br/>32 bits<br/>Ordering]
H4[Ack Number<br/>32 bits<br/>Reliability]
H5[Header Length<br/>4 bits]
H6[Flags<br/>SYN/ACK/FIN<br/>9 bits]
H7[Window Size<br/>16 bits<br/>Flow Control]
H8[Checksum<br/>16 bits]
H9[Options<br/>0-40 bytes]
end
subgraph "Data"
D[Application Payload]
end
end
H1 --> D
H2 --> D
H3 --> D
H4 --> D
H5 --> D
H6 --> D
H7 --> D
H8 --> D
H9 --> D
style H1 fill:#2C3E50,stroke:#16A085,color:#fff
style H2 fill:#2C3E50,stroke:#16A085,color:#fff
style H3 fill:#E67E22,stroke:#16A085,color:#fff
style H4 fill:#E67E22,stroke:#16A085,color:#fff
style H5 fill:#2C3E50,stroke:#16A085,color:#fff
style H6 fill:#E67E22,stroke:#16A085,color:#fff
style H7 fill:#16A085,stroke:#2C3E50,color:#fff
style H8 fill:#2C3E50,stroke:#16A085,color:#fff
style H9 fill:#2C3E50,stroke:#16A085,color:#fff
style D fill:#27ae60,stroke:#16a085,color:#fff
TCP Header Fields (simplified):
| Field | Size | Purpose |
|---|---|---|
| Source/Dest Port | 16 bits each | Application endpoints |
| Sequence Number | 32 bits | Byte stream position |
| Acknowledgment Number | 32 bits | Next expected byte |
| Flags | 9 bits | SYN, ACK, FIN, RST, PSH, URG |
| Window Size | 16 bits | Flow control (receiver buffer) |
| Checksum | 16 bits | Error detection |
| Options | 0-40 bytes | MSS, timestamps, window scaling |
Total Header: 20-60 bytes (vs 8 bytes for UDP)
731.2.2 Why TCP for IoT?
Advantages:
- Reliability: Guaranteed delivery (critical for firmware updates)
- Ordering: Data arrives in correct sequence
- Error Recovery: Automatic retransmission
- Flow Control: Prevents buffer overflow
- Congestion Control: Network-friendly
Disadvantages:
- High Overhead: 20-60 byte header + connection state
- Higher Latency: 3-way handshake before data, retransmission delays
- Power Consumption: Connection state, acknowledgments, retransmissions
- Memory: Connection state requires RAM
- Not Real-Time: Retransmissions cause variable latency
IoT Applications Using TCP: - MQTT (Message Queue Telemetry Transport): Pub/sub messaging - HTTP/HTTPS: Web APIs, RESTful services - Firmware Updates: OTA (Over-The-Air) updates - Configuration: Device configuration and management - File Transfer: Log files, images
The TCP/IP model simplifies the OSI reference model into four practical layers. The transport layer (TCP/UDP) provides end-to-end communication, while the internet layer (IP) handles addressing and routing across networks.
The three-way handshake ensures both endpoints are ready to communicate and synchronizes sequence numbers. For IoT, this overhead of 3 packets before any data transfer makes TCP costly for infrequent sensor readings.
TCP provides reliability through acknowledgments, sequencing, and retransmission but at the cost of overhead and latency. UDP offers minimal overhead and low latency but provides no delivery guarantees. IoT protocols like CoAP build reliability on top of UDP when needed.
Beyond TCP and UDP, modern transport protocols include QUIC (Google’s UDP-based protocol with built-in TLS, used by HTTP/3), SCTP (multi-stream with failover), and DCCP (datagram with congestion control). For IoT, understanding when to use each helps optimize performance.
UDP’s connectionless model means each datagram is independent. The sender fires off datagrams without waiting for acknowledgments, and the receiver processes them as they arrive. This simplicity makes UDP ideal for real-time applications where occasional loss is acceptable.
731.2.3 TCP Analogy
TCP is similar to a telephone system:
- Dial number (SYN - request connection)
- Other person answers (SYN-ACK - acknowledge request)
- You say hello (ACK - confirm connection)
- Connection established - can now talk
- Conversation - back-and-forth communication
- “Did you hear me?” - acknowledgments
- Repeat if needed - retransmission
- “Goodbye” (FIN - close connection)
- “Goodbye back” (FIN-ACK - confirm closure)
Both ends must be connected before communication can begin.
731.3 TCP vs UDP Comparison
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor':'#2C3E50','primaryTextColor':'#fff','primaryBorderColor':'#16A085','lineColor':'#16A085','secondaryColor':'#E67E22','tertiaryColor':'#ecf0f1','background':'#ffffff','mainBkg':'#2C3E50','secondBkg':'#16A085','tertiaryBorderColor':'#95a5a6','clusterBkg':'#ecf0f1','clusterBorder':'#95a5a6','titleColor':'#2C3E50','edgeLabelBackground':'#ffffff','nodeTextColor':'#2C3E50'}}}%%
graph TB
subgraph "TCP Characteristics"
T1[Connection-Oriented<br/>3-way handshake]
T2[Reliable<br/>ACKs + Retransmission]
T3[Ordered<br/>Sequence numbers]
T4[Flow Control<br/>Window size]
T5[Header: 20-60 bytes]
T6[Higher Power]
end
subgraph "UDP Characteristics"
U1[Connectionless<br/>No handshake]
U2[Best-Effort<br/>No guarantees]
U3[Unordered<br/>No sequencing]
U4[No Flow Control]
U5[Header: 8 bytes]
U6[Lower Power]
end
subgraph "Best Use Cases"
TC[TCP:<br/>Firmware Updates<br/>Configuration<br/>MQTT]
UC[UDP:<br/>Sensor Readings<br/>CoAP<br/>Video Streaming]
end
T1 & T2 & T3 & T4 & T5 & T6 --> TC
U1 & U2 & U3 & U4 & U5 & U6 --> UC
style T1 fill:#2C3E50,stroke:#16A085,color:#fff
style T2 fill:#2C3E50,stroke:#16A085,color:#fff
style T3 fill:#2C3E50,stroke:#16A085,color:#fff
style T4 fill:#2C3E50,stroke:#16A085,color:#fff
style T5 fill:#e74c3c,stroke:#c0392b,color:#fff
style T6 fill:#e74c3c,stroke:#c0392b,color:#fff
style U1 fill:#16A085,stroke:#2C3E50,color:#fff
style U2 fill:#16A085,stroke:#2C3E50,color:#fff
style U3 fill:#16A085,stroke:#2C3E50,color:#fff
style U4 fill:#16A085,stroke:#2C3E50,color:#fff
style U5 fill:#27ae60,stroke:#16a085,color:#fff
style U6 fill:#27ae60,stroke:#16a085,color:#fff
style TC fill:#E67E22,stroke:#16A085,color:#fff
style UC fill:#E67E22,stroke:#16A085,color:#fff
731.3.1 Detailed Comparison Table
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (handshake) | Connectionless |
| Reliability | Reliable (ACKs, retransmission) | Best-effort (no guarantees) |
| Ordering | Ordered delivery | Unordered (may arrive out of sequence) |
| Header Size | 20-60 bytes | 8 bytes |
| Overhead | High (ACKs, state, retrans) | Low (fire and forget) |
| Speed | Slower (handshake, ACKs) | Faster (immediate transmission) |
| Latency | Higher (variable due to retrans) | Lower (predictable) |
| Flow Control | Yes (sliding window) | No |
| Congestion Control | Yes (slow start, etc.) | No |
| Error Recovery | Automatic retransmission | None (app must handle) |
| Broadcast/Multicast | No (point-to-point only) | Yes |
| Power Consumption | Higher (state, ACKs) | Lower (minimal processing) |
| Memory Usage | Higher (connection state) | Lower (stateless) |
| Best For | Reliability critical | Low latency, real-time |
| IoT Use Cases | Firmware, config, MQTT | CoAP, telemetry, streaming |
This variant shows how TCP and UDP behave differently during network congestion - critical for understanding IoT performance during high-traffic periods.
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22', 'secondaryColor': '#16A085', 'tertiaryColor': '#E8F6F3', 'fontSize': '10px'}}}%%
sequenceDiagram
box rgb(46, 125, 50) TCP Congestion Control
participant TS as Sensor
participant TN as Network
participant TG as Gateway
end
box rgb(231, 76, 60) UDP No Congestion Control
participant US as Sensor
participant UN as Network
participant UG as Gateway
end
Note over TS,TG: TCP: Backs off when congested
TS->>TN: Data (window=4)
TN--xTG: Congestion detected!
TN->>TS: ACK (window=2)
Note over TS: Reduce rate by 50%
TS->>TN: Data (window=2)
TN->>TG: OK
Note over TS,TG: TCP adapts, network recovers
Note over US,UG: UDP: Ignores congestion
US->>UN: Data 1
US->>UN: Data 2
US->>UN: Data 3
US->>UN: Data 4
UN--xUG: Packets 2,3 dropped!
UN->>UG: Data 1, 4
Note over US,UG: UDP keeps sending<br/>Congestion persists
Key Insight: TCP’s congestion control is “network-friendly” but adds latency. For IoT with bursty traffic, UDP with application-level rate limiting often provides better predictable performance.
731.3.2 When to Use TCP vs UDP in IoT
731.4 Decision Guide
Use TCP when: - Reliability is critical: Firmware updates, configuration - Data must be ordered: Sequential commands, file transfers - Data loss unacceptable: Financial transactions, critical commands - Network is reliable: Wired connections, stable Wi-Fi - Power not a constraint: Mains-powered devices
Use UDP when: - Low latency required: Real-time monitoring, video streaming - Periodic data: Sensor readings every N seconds - Data loss tolerable: Occasional reading loss OK - Broadcast/multicast needed: One-to-many communication - Power constrained: Battery-powered sensors - Overhead matters: 6LoWPAN, constrained networks
Consider hybrid approach: - UDP for telemetry (sensor data) - TCP for critical ops (firmware, config) - Application-level reliability on UDP (CoAP confirmable messages)
731.5 Interactive Tool: TCP vs UDP Protocol Selector
Use this interactive tool to help determine whether TCP or UDP is more appropriate for your IoT application. Select the requirements that apply to your use case, and the tool will recommend a protocol based on a weighted scoring system.
731.6 Visual Reference Gallery
Transport protocols provide the foundation for IoT communication, with TCP offering reliability and UDP offering efficiency for different use cases.
Choosing between TCP and UDP depends on reliability requirements, latency constraints, and power consumption considerations for IoT devices.
The TCP three-way handshake establishes reliable connections but adds latency and overhead, making it less suitable for frequent short transmissions.