2  Transport Protocols: Fundamentals

In 60 Seconds

The transport layer provides end-to-end communication between IoT applications using three key protocols: TCP (reliable, ordered delivery with higher overhead), UDP (lightweight, fire-and-forget with minimal overhead), and DTLS (TLS-equivalent security for UDP traffic). Choosing the right transport protocol based on data criticality, power budget, and network conditions is one of the most consequential decisions in IoT system design.

2.1 Transport Layer Protocols for IoT

The transport layer (Layer 4) provides end-to-end communication between IoT applications. This guide covers TCP, UDP, and DTLS – the three protocols that form the backbone of IoT networking. Understanding when to use each protocol is critical for balancing reliability, latency, and power consumption in your IoT deployments.

Minimum Viable Understanding (MVU)

If you only have 5 minutes, grasp these three essential points:

  • TCP vs UDP is about trade-offs, not superiority: TCP guarantees delivery (20-byte header, connection setup, acknowledgments) while UDP offers lightweight, fire-and-forget delivery (8-byte header). For battery-powered IoT sensors, choosing UDP over TCP can extend battery life from weeks to years.
  • Protocol selection depends on loss tolerance: Use UDP for periodic telemetry and real-time streams where missing one reading is acceptable. Use TCP for firmware updates, actuator commands, and any data where loss means device failure or safety risk.
  • DTLS bridges security and efficiency: DTLS provides TLS-equivalent encryption and authentication for UDP traffic without adding TCP’s reliability overhead – making it the standard security layer for CoAP and other UDP-based IoT protocols.
Learning Objectives

By completing all five chapters, you will be able to:

  • Identify the role of transport layer protocols in the IoT protocol stack
  • Compare TCP and UDP characteristics, headers, and use cases
  • Explain why UDP is preferred for periodic sensor telemetry in IoT applications
  • Distinguish DTLS from TLS and justify when DTLS should secure UDP traffic
  • Analyze trade-offs between reliability, overhead, and power consumption
  • Select the appropriate transport protocol for different IoT scenarios
  • Implement reliable message transport systems using TCP and UDP
  • Diagnose common pitfalls in transport protocol selection and apply corrective strategies

2.2 Sensor Squad: Transport Protocols for Kids!

Meet the Sensor Squad!

  • Sammy the Sensor – A curious temperature sensor who always wants his readings to arrive safely
  • Lila the Light – A cheerful light sensor who sends brightness updates super fast
  • Max the Motor – An energetic actuator who needs to receive the right instructions every time
  • Bella the Button – A helpful input device who always confirms when something is done

Sammy says: “Hey friends! I need to send my temperature reading to the cloud. But I have two ways to mail it – which one should I pick?”

Lila asks: “What are the two ways, Sammy?”

Sammy explains: “Okay, imagine sending a letter to your friend!”

2.2.1 UDP: The Postcard Way

Sammy says: “UDP is like writing my temperature on a postcard and dropping it in the mailbox. It is super fast – I just throw it in and walk away! But there is no tracking, so I do not know if it arrived. And that is okay! If my friend misses one postcard, I will send another one in 5 minutes anyway.”

Lila adds: “I use postcards too! I send brightness levels every second. If one gets lost, the next one is already on its way!”

2.2.2 TCP: The Registered Mail Way

Max says: “But wait! When the cloud sends me an instruction to open a valve, I need TCP – that is like registered mail with a signature. The mail carrier knocks on my door, I sign for it, and only then does the sender know I got the message. It takes longer, but I am SURE I have the right instructions!”

Bella adds: “And for firmware updates – those big software downloads – we ALWAYS use registered mail. If even one page of the instructions is missing, the whole thing could go wrong!”

2.2.3 The Big Idea

Sammy wraps up: “So remember, friends:”

Method Like… Good For
UDP (Postcard) Quick, no tracking Temperature, light, humidity readings
TCP (Registered Mail) Slow but guaranteed Firmware updates, important commands

Max shouts: “Pick the right delivery method for the right job – that is what smart IoT engineers do!”


2.3 For Beginners: Understanding Transport Protocols

Analogy: Transport protocols are like different shipping services for your packages. Just as you choose between express courier (fast, tracked, guaranteed delivery) and standard mail (cheaper, no tracking, might get lost), IoT devices choose between TCP and UDP based on what they are sending.

2.3.1 The Three Key Protocols

  1. UDP (User Datagram Protocol) – The “fire and forget” option. Your device sends data and moves on. No handshake, no confirmation, no waiting. Perfect when speed matters more than guaranteed delivery.

  2. TCP (Transmission Control Protocol) – The “reliable delivery” option. Before sending anything, devices shake hands to establish a connection. Every piece of data gets acknowledged. If something gets lost, it gets resent automatically.

  3. DTLS (Datagram Transport Layer Security) – Think of this as “UDP wearing armor.” It adds encryption and authentication to UDP without adding TCP’s heavyweight reliability machinery.

2.3.2 Why Does This Matter for IoT?

Battery-powered sensors live or die by their energy budget. Every extra byte sent and every round-trip waiting for a reply drains the battery:

  • TCP handshake = 1.5 round trips before any data flows (SYN, SYN-ACK, ACK)
  • UDP = zero round trips, just send the data
  • For a sensor sending one reading every 15 minutes, TCP can consume 10x or more energy than UDP

2.3.3 The Simple Decision Rule

Question Answer
Can you afford to lose an occasional reading? Yes -> UDP
Must every byte arrive perfectly? Yes -> TCP
Need encryption without TCP overhead? Yes -> DTLS

That is the fundamental transport protocol choice in IoT!


2.4 Transport Layer in the IoT Protocol Stack

The transport layer sits between the application layer (where your IoT logic runs) and the network layer (where packets get routed). Its job is to provide end-to-end communication services to the applications above it.

Diagram showing the IoT protocol stack with the Application layer at the top containing CoAP, MQTT, and HTTP, the Transport layer in the middle containing TCP, UDP, and DTLS, the Network layer below containing IPv6 and 6LoWPAN, and the Link layer at the bottom containing IEEE 802.15.4, BLE, and Wi-Fi, with arrows showing data flow between layers using IEEE navy, teal, and orange colors

Transport Layer Position in the IoT Protocol Stack
Figure 2.1: Transport Layer Position in the IoT Protocol Stack

The transport layer is highlighted because it is the critical decision point for IoT architects. Application protocols like CoAP run over UDP, while MQTT runs over TCP – and this choice cascades into power consumption, latency, and reliability characteristics of the entire system.

2.5 TCP vs UDP: The Core Decision

Every IoT deployment begins with a fundamental transport protocol choice. The diagram below shows the key architectural differences.

Comparison diagram showing TCP on the left with connection setup through 3-way handshake, data transfer with acknowledgments, flow control with sliding window, and connection teardown through 4-way close, versus UDP on the right with no connection setup, direct data sending without acknowledgments, no flow control, and no teardown, highlighting the overhead difference using IEEE colors

TCP vs UDP Architectural Comparison
Figure 2.2: TCP vs UDP Architectural Comparison

2.6 Protocol Selection Decision Framework

Choosing the right transport protocol requires evaluating multiple factors. Use this decision tree to guide your selection.

Decision tree flowchart starting with the question Is data loss acceptable, branching to UDP path if yes and TCP path if no, with further branches for security needs leading to DTLS for secure UDP and TLS for secure TCP, and final protocol recommendations mapping to CoAP over UDP, MQTT over TCP, and HTTPS over TLS using IEEE colors

IoT Transport Protocol Selection Decision Tree
Figure 2.3: IoT Transport Protocol Selection Decision Tree

2.7 Byte-Level Header Comparison

Understanding the header overhead is critical for IoT power budgeting. Every byte costs energy to transmit.

Block diagram comparing the header structures of UDP with 4 fields totaling 8 bytes, TCP with 10 or more fields totaling 20 to 60 bytes, and DTLS adding 13 or more bytes of security overhead to UDP, with each field labeled and sized, using IEEE navy teal and orange colors

Header Size Comparison: UDP (8 bytes) vs TCP (20-60 bytes) vs DTLS (13+ bytes added to UDP)
Figure 2.4: Header Size Comparison: UDP (8 bytes) vs TCP (20-60 bytes) vs DTLS (13+ bytes added to UDP)

Key Takeaway: For a 24-byte sensor payload, UDP adds just 8 bytes (25% overhead), while TCP adds 20+ bytes (83%+ overhead) – before accounting for connection setup packets. For thousands of sensors sending readings every few minutes, this difference translates directly into power consumption and network capacity.

2.7.1 Interactive: TCP vs UDP Overhead Calculator

Adjust the payload size to see how the overhead ratio changes between TCP (with full connection lifecycle) and UDP.

2.8 Chapter Navigation

This guide is split into five focused chapters:

2.8.1 1. Transport Protocols Overview (~2,450 words)

Introduction to transport layer protocols, prerequisites, and beginner-friendly explanations of TCP vs UDP. Includes video resources, analogies, and the Sensor Squad story for kids.

2.8.2 2. User Datagram Protocol (UDP) (~1,700 words)

Deep dive into UDP characteristics, header structure, and IoT applications. Covers connectionless communication, checksum importance, and knowledge check quizzes.

2.8.3 3. Transmission Control Protocol (TCP) (~3,230 words)

Comprehensive TCP coverage including 3-way handshake, reliable delivery mechanisms, connection state management, and why TCP is essential for firmware updates.

2.8.4 4. TCP vs UDP: Comparison and Selection (~2,210 words)

Detailed feature comparison, engineering tradeoffs, and an interactive protocol selector tool. Includes decision frameworks for choosing between TCP and UDP.

2.8.5 5. Practical Applications and Labs (~7,910 words)

Real-world examples, common mistakes, worked examples with battery calculations, and a hands-on ESP32 lab for building a reliable message transport system.

2.9 Quick Reference

Protocol Header Connection Reliability Best For
UDP 8 bytes Connectionless Best-effort Sensor telemetry, streaming
TCP 20-60 bytes Connection-oriented Guaranteed Firmware, configuration
DTLS 13 bytes added to UDP Session-based Secured UDP Secure IoT telemetry

2.9.1 Interactive: Battery Life Impact Calculator

Explore how transport protocol choice affects battery life across different network technologies.

2.10 Prerequisites

Before starting, you should be familiar with:

2.12 Common Pitfalls in Transport Protocol Selection

Mistakes That Cause Real-World IoT Failures

Pitfall 1: Using TCP for periodic sensor telemetry. Many developers default to TCP because it is “reliable.” But for a temperature sensor sending readings every 5 minutes, TCP’s 3-way handshake (SYN, SYN-ACK, ACK) wastes 1.5 round trips of energy before any data flows. The next reading replaces the current one, so losing one is harmless. Use UDP instead and save 10x or more in energy.

Pitfall 2: Using UDP for firmware updates. Firmware updates have zero tolerance for missing or corrupted data. A single wrong byte can permanently brick a device in the field ($500+ replacement cost). Always use TCP with TLS for firmware delivery, even though it costs more energy. The update happens once a month – the cost is negligible.

Pitfall 3: Forgetting about NAT and firewalls. UDP is connectionless, so NAT mapping entries expire quickly (often in 30-120 seconds). If your sensor only sends data every 15 minutes, the NAT entry will have expired and inbound messages cannot reach the device. Solutions include periodic keep-alive packets or using CoAP with observe patterns.

Pitfall 4: Not disabling Nagle’s algorithm for real-time TCP. Nagle’s algorithm batches small TCP packets for efficiency, adding 200ms+ of latency. For real-time actuator commands, this delay can be dangerous. Always set TCP_NODELAY when latency matters more than throughput.

Pitfall 5: Assuming DTLS and TLS are interchangeable. DTLS is designed for UDP (handles out-of-order and lost packets at the security layer). TLS is designed for TCP (assumes reliable, ordered delivery). Using the wrong one causes either security failures or broken connections.


2.13 Worked Example: Choosing Transport for a Smart Agriculture System

Step-by-Step Protocol Selection

Scenario: You are designing a smart agriculture system with three types of IoT traffic:

  1. Soil moisture sensors – 500 sensors, each sending a 24-byte reading every 15 minutes over NB-IoT
  2. Irrigation valve commands – Cloud sends open/close commands to 50 valves that control water flow
  3. Firmware updates – Monthly 64 KB update pushed to all 500 sensors

Step 1: Classify each traffic type by loss tolerance

Traffic Loss Tolerance Consequence of Loss
Soil moisture readings High – next reading in 15 min Slightly stale data; no harm
Valve open/close commands Zero – water damage risk Crops flooded or dried out
Firmware updates Zero – device bricking risk $500+ per bricked sensor

TCP vs UDP Overhead: Quantifying the Energy Cost

For 500 soil moisture sensors sending 24-byte readings every 15 minutes over 1 year via NB-IoT (TX: 200 mA, 50 kbps):

\[ \text{UDP packet size} = 24 + 8 = 32 \text{ bytes} \quad \Rightarrow \quad T_{\text{TX}} = \frac{32 \times 8}{50{,}000} = 5.12 \text{ ms} \]

\[ E_{\text{UDP}} = 200 \text{ mA} \times 5.12 \text{ ms} = 1.024 \text{ mAs} = 0.000284 \text{ mAh per reading} \]

TCP requires setup (SYN: 40B, SYN-ACK: 40B, ACK: 40B), data+ACK (44B + 40B), teardown (4 × 40B):

\[ \text{TCP total size} = 120 + 44 + 40 + 160 = 364 \text{ bytes} \quad \Rightarrow \quad T_{\text{TX}} = \frac{364 \times 8}{50{,}000} = 58.24 \text{ ms} \]

\[ E_{\text{TCP}} = 200 \text{ mA} \times 58.24 \text{ ms} = 11.648 \text{ mAs} = 0.003236 \text{ mAh per reading} \]

Over 1 year (35,040 readings per sensor), cumulative energy per sensor:

\[ \Delta E = 35{,}040 \times (0.003236 - 0.000284) = 103.4 \text{ mAh} \]

With a 5,000 mAh battery, TCP overhead alone reduces battery life by 2.1% compared to UDP—or 76 fewer days of operation. For 500 sensors, choosing TCP wastes 51.7 Ah of total fleet capacity annually.

Step 2: Calculate overhead for soil moisture (the high-volume case)

UDP approach:

  • Per reading: 24 bytes payload + 8 bytes UDP header = 32 bytes
  • 500 sensors x 96 readings/day = 48,000 packets/day
  • Total: 48,000 x 32 = 1,536,000 bytes/day (1.5 MB)

TCP approach:

  • Connection setup: SYN (40B) + SYN-ACK (40B) + ACK (40B) = 120 bytes
  • Data: 24 bytes payload + 20 bytes TCP header = 44 bytes
  • ACK: 40 bytes
  • Teardown: FIN (40B) + ACK (40B) + FIN (40B) + ACK (40B) = 160 bytes
  • Per reading total: 120 + 44 + 40 + 160 = 364 bytes
  • Total: 48,000 x 364 = 17,472,000 bytes/day (17.5 MB)

TCP costs 11.4x more bandwidth than UDP for the same telemetry data.

Step 3: Select protocols

Traffic Protocol Reason
Soil moisture UDP (CoAP NON) Loss-tolerant, 11.4x less overhead
Valve commands TCP with TLS Zero loss tolerance, critical commands
Firmware updates TCP with TLS Zero loss tolerance, integrity required

Step 4: Address edge cases

  • Soil sensors use CoAP with observe for server-initiated alerts (e.g., “moisture critically low”)
  • Valve commands use TCP_NODELAY to avoid Nagle’s algorithm latency
  • Firmware uses block-wise transfer with SHA-256 integrity checks per block

Result: The system uses UDP for 96% of its traffic (telemetry) and TCP for only 4% (commands + firmware), saving approximately 90% of radio energy budget.


2.14 Knowledge Check: Transport Protocol Fundamentals

Test your understanding of the core transport protocol concepts covered in this guide.


2.15 Enhanced Summary

This guide provides the foundational knowledge for selecting and using transport protocols in IoT systems. Here is what you should take away:

2.15.1 Key Concepts

Concept Summary
UDP Connectionless, 8-byte header, no reliability guarantees. Ideal for periodic telemetry where occasional loss is acceptable.
TCP Connection-oriented, 20-60 byte header, guaranteed ordered delivery. Required for firmware updates, critical commands, and financial data.
DTLS Security layer for UDP. Provides TLS-equivalent encryption without TCP’s reliability overhead. Standard for CoAP security.
Trade-off TCP costs up to 11.4x more bytes than UDP for a single sensor reading. For battery-powered devices, this translates directly to battery life.
Decision Rule Loss-tolerant periodic data uses UDP. Zero-loss-tolerance data uses TCP. Add DTLS or TLS when encryption is required.

2.15.2 Design Principles

  1. Default to UDP for sensor telemetry – the most common IoT traffic pattern
  2. Reserve TCP for operations where data loss means device failure or safety risk
  3. Always enable checksums – even UDP’s 2-byte checksum catches dangerous silent corruption
  4. Consider NAT behavior – UDP NAT mappings expire quickly; plan for keep-alives or alternative patterns
  5. Profile before optimizing – measure actual power consumption with each protocol on your specific hardware and network
Cross-Hub Connections

Scenario: A smart parking sensor sends occupancy status (occupied=1, vacant=0) every 2 minutes. The payload is tiny: 1 byte for status + 4 bytes timestamp = 5 bytes total.

Network: Wi-Fi (802.11g) with IPv4

Question 1: Calculate total bytes transmitted per reading using UDP

UDP Stack:

  • Ethernet header: 14 bytes
  • IPv4 header: 20 bytes
  • UDP header: 8 bytes
  • Payload: 5 bytes
  • Total: 47 bytes

Efficiency: 5 / 47 = 10.6% (89.4% overhead)

Question 2: Calculate total bytes for TCP with connection per reading

TCP Stack (new connection each time):

Handshake (3-way):

  • SYN: 14B (Ethernet) + 20B (IPv4) + 20B (TCP) = 54 bytes
  • SYN-ACK: 54 bytes
  • ACK: 54 bytes
  • Handshake total: 162 bytes

Data transmission:

  • Data + ACK: 14B + 20B + 20B + 5B = 59 bytes
  • ACK response: 54 bytes
  • Data total: 113 bytes

Connection teardown (4-way close):

  • FIN: 54 bytes
  • ACK: 54 bytes
  • FIN: 54 bytes
  • ACK: 54 bytes
  • Teardown total: 216 bytes

Grand total: 162 + 113 + 216 = 491 bytes

Efficiency: 5 / 491 = 1.0% (99% overhead!)

Overhead ratio: TCP uses 491 / 47 = 10.4× more bytes than UDP

Question 3: What if TCP connection is kept alive?

TCP with persistent connection:

  • Handshake: 162 bytes (one-time cost)
  • Per reading: 59 bytes (data) + 54 bytes (ACK) = 113 bytes
  • Teardown: 216 bytes (one-time at end)

For a 1-hour session with readings every 2 minutes: - Readings: 30 per hour - Handshake: 162 bytes (once) - Data: 30 × 113 = 3,390 bytes - Teardown: 216 bytes (once) - Total: 3,768 bytes for 30 readings

Per-reading average: 3,768 / 30 = 125.6 bytes

UDP for same 30 readings: 30 × 47 = 1,410 bytes

Overhead ratio (persistent TCP vs UDP): 3,768 / 1,410 = 2.67×

Even with persistent connection, TCP still uses 2.67× more bytes than UDP.

Question 4: Calculate battery impact

Battery: 2500 mAh lithium cell (e.g., 18650 or AA lithium) Wi-Fi TX current: 180 mA Wi-Fi data rate: 54 Mbps (802.11g) Sleep current: 10 μA

UDP transmission time per reading:

  • Bits: 47 × 8 = 376 bits
  • Time: 376 / 54,000,000 = 7 microseconds
  • Energy: 180 mA × 7 μs = 0.00035 μAh

TCP transmission time per reading (persistent connection):

  • Bits: 113 × 8 = 904 bits
  • Time: 904 / 54,000,000 = 16.7 microseconds
  • Energy: 180 mA × 16.7 μs = 0.00083 μAh

Daily energy comparison:

  • Readings per day: 720 (every 2 minutes)
  • UDP daily TX: 720 × 0.00035 μAh = 0.25 μAh
  • TCP daily TX: 720 × 0.00083 μAh = 0.60 μAh
  • Sleep energy: 10 μA × 24 hours = 240 μAh

Battery life:

  • UDP: 2,500,000 / (0.25 + 240) = 10,416 days = 28.5 years
  • TCP: 2,500,000 / (0.60 + 240) = 10,395 days = 28.5 years

Key Insight: For Wi-Fi at 54 Mbps, transmission is so fast (microseconds) that sleep current dominates battery life. Protocol overhead doesn’t matter much because TX time is negligible.

Question 5: What if this was NB-IoT at 30 kbps instead?

UDP transmission time:

  • Bits: 47 × 8 = 376 bits
  • Time: 376 / 30,000 = 12.5 milliseconds
  • Energy: 120 mA × 12.5 ms = 0.42 μAh

TCP transmission time (persistent):

  • Bits: 113 × 8 = 904 bits
  • Time: 904 / 30,000 = 30.1 milliseconds
  • Energy: 120 mA × 30.1 ms = 1.00 μAh

Daily energy (NB-IoT):

  • UDP: 720 × 0.42 μAh = 302 μAh
  • TCP: 720 × 1.00 μAh = 720 μAh
  • Sleep: 5 μA × 24 hours = 120 μAh

Battery life (NB-IoT):

  • UDP: 2,500,000 / (302 + 120) = 5,924 days = 16.2 years
  • TCP: 2,500,000 / (720 + 120) = 2,976 days = 8.2 years

Comparison:

  • Wi-Fi: Protocol overhead doesn’t matter (28.5 years either way)
  • NB-IoT: TCP reduces battery life by 50% (16.2 → 8.2 years)

Lesson Learned: Protocol overhead impact depends on data rate: - High-speed networks (Wi-Fi, LTE): Sleep dominates, protocol choice matters less - Low-speed networks (NB-IoT, LoRa): TX time dominates, every byte counts - use UDP

Decision Rule: If data rate < 100 kbps AND battery-powered, always use UDP for small, frequent telemetry.

2.16 Concept Relationships

Builds Upon:

  • IP (network layer) routing → Transport adds end-to-end communication semantics
  • Socket abstraction maps protocols to application APIs

Enables:

  • MQTT: Runs on TCP for reliable messaging
  • CoAP: Runs on UDP for lightweight IoT
  • DTLS Security: Secures UDP communications

Related Concepts:

  • Port multiplexing allows multiple applications per device
  • Flow control prevents receiver buffer overflow
  • Congestion control prevents network collapse

2.17 See Also

Reliability:

Security:

2.18 Try It Yourself

Exercise 1: Measure TCP vs UDP Overhead

Use Wireshark/tcpdump to capture and compare actual packet sizes for 50-byte payload. Expected: UDP = 92 bytes total (14B Ethernet + 20B IPv4 + 8B UDP + 50B payload); TCP new-connection = 536 bytes total (3-way handshake: 162B + data+ACK: 158B + 4-way teardown: 216B), giving a ratio of 536/92 = 5.8x overhead.

Exercise 2: Battery Impact Calculation

Calculate how protocol choice affects battery life for your specific sensor deployment using the battery calculator template in the worked example.

Exercise 3: Protocol Selection Decision

Answer 5 key questions (loss tolerance, latency, battery, payload size, bidirectional needs) to systematically choose TCP vs UDP for your application.

2.19 What’s Next

Chapter Focus Why Read It
Transport Protocols Overview Foundations, analogies, protocol stack context Start here to build the conceptual model before exploring individual protocols
User Datagram Protocol (UDP) Header structure, connectionless design, IoT use cases Deep-dive into UDP mechanics and why it dominates IoT telemetry traffic
Transmission Control Protocol (TCP) 3-way handshake, reliability mechanisms, connection state Understand when TCP’s reliability guarantees justify its energy and overhead cost
TCP vs UDP: Comparison and Selection Feature comparison, decision frameworks, interactive selector Apply the trade-off analysis to confidently choose the right protocol
CoAP Protocol Constrained Application Protocol over UDP See how CoAP leverages UDP and adds lightweight request-response semantics
MQTT Protocol Message Queue Telemetry Transport over TCP Explore how MQTT uses TCP’s reliability for publish-subscribe IoT messaging