Transport protocols (Layer 4) manage end-to-end communication between IoT devices. TCP guarantees reliable, ordered delivery with a 20-byte minimum header and 3-way handshake, while UDP provides minimal 8-byte header fire-and-forget transmission. For battery-powered IoT sensors sending frequent readings, UDP (or CoAP over UDP) can extend battery life by 10x compared to TCP by eliminating connection setup and acknowledgment overhead.
Key Concepts
OSI Layer 4 Responsibilities: End-to-end data delivery between applications; port-based multiplexing; optional: error detection (both), error recovery (TCP), ordering (TCP), flow control (TCP), congestion control (TCP)
TCP Segment: Unit of TCP transmission including: source port, dest port, sequence number, ACK number, header length, flags (SYN/ACK/FIN/RST/PSH/URG), window size, checksum, urgent pointer, options, payload
UDP Datagram: Minimal transport unit: 8-byte header (source port, dest port, length, checksum) + payload; no connection state, no ordering, no reliability
Internet Checksum: 16-bit ones-complement sum of UDP/TCP header + data (IPv4 optional for UDP, mandatory for IPv6); detects single-bit errors; NOT cryptographically strong
TCP Slow Start: Congestion control phase starting from cwnd=1 MSS; doubles cwnd per RTT until ssthresh; transitions to Congestion Avoidance (linear increase) at ssthresh
TCP Fast Retransmit: Retransmits lost segment immediately upon receiving 3 duplicate ACKs without waiting for retransmission timeout; faster recovery than RTO-based retransmission
UDP Multicast Group: IPv4 224.0.0.0/4 or IPv6 ff00::/8 address ranges; UDP datagrams sent to multicast address are delivered to all group members; used for IoT device discovery (mDNS, SSDP)
Port Scanning: Network reconnaissance technique sending connection attempts to all ports; TCP port scan: SYN → SYN-ACK (open) or RST (closed); IoT devices must use firewalls to block unauthorized port access
Learning Objectives
By the end of this section, you will be able to:
Identify the role of transport layer protocols (Layer 4) within the IoT protocol stack
Compare TCP and UDP characteristics and use cases across IoT deployment scenarios
Explain why UDP is preferred for many IoT applications and justify when TCP is the better choice
Evaluate trade-offs between reliability, overhead, and power consumption for a given IoT scenario
Analyze header structures to calculate protocol overhead and its impact on battery life
8.1 Prerequisites
Before diving into this chapter, you should be familiar with:
Layered Network Models: Understanding the OSI and TCP/IP protocol stack is essential since transport protocols (Layer 4) sit between application protocols and network routing, managing end-to-end communication
Networking Basics: Knowledge of fundamental networking concepts including packets, headers, ports, addressing, and basic data transmission provides the foundation for understanding transport protocol operation
IoT device constraints: Familiarity with power consumption, memory limitations, and bandwidth constraints of IoT sensors helps you appreciate why choosing between TCP and UDP significantly impacts battery life and network efficiency
Key Takeaway
The one question that decides everything: Can I tolerate losing this data point? If yes, use UDP. If no, use TCP.
Remember this: The overhead gap widens with transmission frequency. For hourly reports, TCP and UDP battery life differ by less than 5%. For per-second telemetry, UDP can last 3-6x longer because it skips connection setup and acknowledgments.
Sensor Squad: The Two Delivery Methods!
“Think of TCP and UDP as two very different delivery services,” said Max the Microcontroller. “TCP is like a courier who makes you sign for every package, calls if the delivery fails, and keeps trying until you get it. Reliable, but slow and expensive.”
“UDP is more like throwing a paper airplane across the room,” laughed Sammy the Sensor. “You launch it and hope it arrives! No confirmation, no retries. But it is incredibly fast and uses almost no energy – just an 8-byte header versus TCP’s 20-byte minimum.”
“For my temperature readings, UDP is perfect,” added Lila the LED. “I send a reading every minute. If one gets lost, another is coming soon. But if someone sends a firmware update to reprogram me, they better use TCP – losing part of the update could brick me!”
“The numbers tell the story,” said Bella the Battery. “For a 10-byte sensor payload, TCP adds 20 bytes of header plus the handshake overhead. That is 200 percent overhead! UDP adds just 8 bytes – only 80 percent overhead. Over thousands of transmissions, that difference means months of extra battery life.”
For Beginners: Transport Protocols
What are Transport Protocols? Transport protocols are the delivery systems that move data between applications running on different devices. The two main options are TCP (reliable but slower, like registered mail with tracking) and UDP (fast but unreliable, like dropping a postcard in the mailbox). For IoT, choosing the right transport protocol can mean the difference between a battery lasting 6 months versus 6 years.
Why does it matter? Every time your smart thermostat sends a temperature reading or your fitness tracker uploads step counts, it uses either TCP or UDP. TCP guarantees the data arrives correctly but uses more battery power and time due to acknowledgments and retransmissions. UDP sends data quickly with minimal overhead but doesn’t guarantee delivery. Understanding this trade-off helps you design efficient IoT systems - use UDP for frequent sensor readings where losing one data point doesn’t matter, and TCP for critical commands like unlocking a smart door.
Key terms to know: | Term | Simple Definition | |——|——————-| | TCP | Transmission Control Protocol - reliable delivery with acknowledgments, like certified mail | | UDP | User Datagram Protocol - fast, connectionless delivery with no guarantees, like postcards | | 3-way handshake | TCP’s connection setup process (SYN, SYN-ACK, ACK) required before sending data | | Retransmission | When TCP automatically resends lost packets, improving reliability but draining battery | | DTLS | Datagram Transport Layer Security - encrypted version of UDP for secure, lightweight communication |
Time: ~10 min | Level: Foundational | Unit: P07.C33.U01
The transport layer (Layer 4 in OSI model) provides end-to-end communication between applications. While not IoT-specific, transport protocols are crucial for IoT applications.
Figure 8.1: Transport layer protocols (TCP, UDP, DTLS) connecting application and network layers
Transport layer protocols sit between application and network layers, providing end-to-end communication services
Common Misconception: “TCP Always Uses More Power Than UDP”
The Misconception: Many developers assume TCP always consumes significantly more power than UDP for IoT devices, leading them to avoid TCP entirely.
The Reality: For infrequent transmissions (every 5+ minutes), TCP and UDP have nearly identical battery life. A real-world study of Nordic nRF52840 sensors sending 10-byte readings every 5 minutes showed:
UDP: 45.6 years battery life (2000 mAh)
TCP (full connection): 43.8 years battery life
Difference: Only 4% (~1.8 years over 45 years)
Why? At low transmission frequencies, sleep current (5 uA) dominates battery consumption (99.99% of time). TCP’s connection overhead (13 ms vs 1.3 ms) is only 0.00027% of each 5-minute cycle.
When TCP Overhead Matters: The power penalty appears at high transmission rates (every 10-30 seconds). The same study showed at 10-second intervals:
UDP: 45.5 years
TCP (full connection): 8.6 years
Difference: 81% reduction (36.9 years)
Quantified Insight: TCP overhead becomes significant when active radio time exceeds ~0.1% of the duty cycle. Below that threshold, sleep current dominates regardless of protocol choice.
Takeaway: Don’t blindly avoid TCP for battery devices. Calculate your specific duty cycle - for infrequent reporting (hourly/daily), TCP’s reliability benefits often outweigh negligible power costs.
Core Transport Protocols for IoT
IoT applications primarily use three transport layer protocols:
UDP (User Datagram Protocol): Connectionless, lightweight, no guarantees
TCP (Transmission Control Protocol): Connection-oriented, reliable, higher overhead
DTLS (Datagram Transport Layer Security): Secure UDP communication
8.3 User Datagram Protocol (UDP)
UDP is a connectionless protocol with very low overhead and fast transmission, but no guarantee for message delivery.
8.3.1 UDP Characteristics
Figure 8.2: UDP connectionless transmission showing packet loss and reordering without acknowledgments
UDP is connectionless: datagrams are sent without handshake, acknowledgment, or guaranteed delivery
Alternative View: UDP Timeline with Energy Focus
This variant shows the same UDP communication but emphasizes the energy savings that make UDP attractive for battery-powered IoT sensors.
Figure 8.3: UDP Energy Timeline - Shows why UDP’s “fire and forget” approach saves significant battery on constrained devices
Key Insight: The energy savings come from eliminating round-trip wait times. Each TCP handshake requires waiting 50-200 ms for responses, keeping the radio active and draining battery.
8.3.2 UDP Properties
Connectionless:
No connection establishment (no handshake)
No connection state maintained
Each datagram independent
Unreliable:
No acknowledgments: Sender doesn’t know if received
No retransmission: Lost packets are not resent
No ordering: Packets may arrive out of order
No flow control: Can overwhelm receiver
Lightweight:
Small header: 8 bytes only
Minimal processing: No state, no retransmission logic
Low latency: Immediate transmission
Use Cases:
Sensor readings (temperature, humidity)
Real-time data (video streaming, voice)
DNS queries
IoT telemetry (CoAP, MQTT-SN)
8.3.3 UDP Header Structure
Figure 8.4: UDP datagram header structure showing 8-byte header with ports, length, and checksum fields
UDP header is minimal at only 8 bytes, providing lightweight transport with low overhead
UDP Header Fields:
Field
Size
Purpose
Source Port
16 bits
Sending application’s port (optional, can be 0)
Destination Port
16 bits
Receiving application’s port
Length
16 bits
Total length (header + data) in bytes
Checksum
16 bits
Error detection (optional in IPv4, mandatory in IPv6)
Total Header: 8 bytes (minimal overhead)
Maximum Payload: 65,507 bytes (65,535 - 20 IP header - 8 UDP header)
8.3.4 Why UDP for IoT?
Advantages:
Low Overhead: 8-byte header vs 20-byte TCP header
Low Power: No connection state = less memory, less processing
Simplicity: Easy to implement on constrained devices
Multicast/Broadcast: UDP supports one-to-many communication
Real-Time: No retransmission delays
Disadvantages:
Unreliable: Packets may be lost
Unordered: Packets may arrive out of sequence
No Congestion Control: Can overwhelm network
IoT Applications Using UDP:
CoAP (Constrained Application Protocol): RESTful for IoT
MQTT-SN (MQTT for Sensor Networks): Pub/sub for sensors
Infrequent transmissions (< 1/minute) where overhead is negligible
Using protocols requiring TCP (MQTT, HTTP, TLS)
Choose UDP when:
Occasional packet loss is acceptable (periodic telemetry)
Low latency is critical (real-time control, streaming)
Battery life is paramount (frequent transmissions)
Using lightweight protocols (CoAP, DNS, mDNS)
Default recommendation: UDP with CoAP Confirmable messages for critical alerts, Non-Confirmable for routine telemetry. Use TCP only for firmware updates, configuration changes, and when MQTT’s features justify the overhead.
Alternative View: TCP vs UDP Packet Timeline Comparison
This variant shows the same data transfer using TCP vs UDP side-by-side, highlighting the time and packet overhead difference - especially critical for understanding IoT battery impact.
Figure 8.8: TCP vs UDP Packet Timeline - Same 20-byte sensor reading requires 8 TCP packets vs 1 UDP packet
Key Insight: For a single sensor reading, TCP requires ~6.6x more bytes transmitted than UDP (344 vs 52 bytes including IP headers). At 1 reading/minute, TCP can reduce battery life by 3x or more compared to UDP.
8.6 Worked Example: Choosing TCP vs UDP for a Smart Agriculture Gateway
Scenario: A farm deploys 200 soil moisture sensors across 50 hectares. Each sensor sends a 24-byte reading (moisture level, temperature, battery voltage) every 60 seconds to a LoRaWAN gateway, which relays data to a cloud dashboard. The gateway has a 4G cellular uplink (1 Mbps) with a $0.01/MB data plan. The same gateway also receives firmware update commands (average 50 KB each) from the cloud once per month.
Question: Should the gateway use TCP or UDP for (a) relaying sensor telemetry to the cloud and (b) receiving firmware commands?
Step 1: Calculate telemetry overhead with TCP vs UDP
Putting Numbers to It
The overhead difference between TCP and UDP directly impacts battery life. For a 24-byte sensor payload transmitted every 60 seconds:
UDP total bytes:\[\text{Total}_{\text{UDP}} = 8\text{ B (UDP)} + 20\text{ B (IP)} + 24\text{ B (payload)} = 52\text{ B}\]
The 6.6x per-transmission energy difference (344 vs 52 bytes) is dampened by sleep current but still results in roughly 2x shorter battery life – significant over a multi-year deployment.
Per sensor reading (24-byte payload):
Protocol
Header
IP Header
Total per Message
Overhead %
UDP
8 bytes
20 bytes
52 bytes
117%
TCP
20 bytes
20 bytes
64 bytes
167%
TCP (new conn)
20 + handshake (3 x 40) + teardown (4 x 40)
20 bytes
344 bytes
1333%
Step 2: Calculate monthly data cost
200 sensors x 1 msg/min x 60 min x 24 hr x 30 days = 8,640,000 messages/month
Protocol
Bytes/Month
Monthly Cost
UDP
8.64M x 52 = 449 MB
$4.49
TCP (persistent)
8.64M x 64 = 553 MB
$5.53
TCP (per-message)
8.64M x 344 = 2,972 MB
$29.72
Step 3: Assess reliability needs
Sensor telemetry: If one reading is lost, the next arrives in 60 seconds. 2% packet loss means missing ~172,800 readings/month out of 8.64 million – insignificant for trend analysis.
Firmware commands: A corrupted or lost firmware update can brick a device. Reliability is mandatory.
Must be complete and uncorrupted, 50 KB/month is negligible overhead
Key insight: The $25.23/month savings from using UDP instead of TCP-per-message may seem small, but across 100 gateways over 5 years, that is $151,380. For battery-powered gateways on solar, the energy savings from eliminating TCP handshakes would be even more significant than the data cost.
8.7 Interactive: TCP vs UDP Overhead Calculator
Use this calculator to compare TCP and UDP overhead for your own IoT scenario. Adjust the payload size, transmission interval, and battery capacity to see how protocol choice affects battery life and data costs.
1. Confusing TCP with UDP Error Detection Capabilities
TCP does not provide stronger error detection than UDP — both use the same 16-bit Internet checksum. TCP’s advantage is error recovery (retransmission on detected error), not better error detection. For a 1,000-byte message, both TCP and UDP have the same ~1-in-65,536 probability of undetected bit errors. Applications requiring high data integrity (firmware images, cryptographic keys) must add their own CRC-32 or SHA-256 verification at the application layer, regardless of transport protocol.
2. Assuming TCP Prevents All Packet Loss
TCP guarantees that bytes are delivered or an error is reported — it does NOT guarantee that bytes are delivered quickly or with bounded latency. During severe network congestion, TCP can reduce throughput to near zero and still report successful delivery (bytes sit in retransmission queues). For real-time IoT applications where data freshness matters more than completeness (live sensor feeds, streaming), “reliable delivery but potentially stale” from TCP is worse than “fresh but occasionally missing” from UDP.
3. Not Understanding TCP Header Options Impact on MTU
A TCP SYN with MSS option, SACK option, window scale option, and timestamp option adds 20–40 bytes of options, reducing the SYN segment’s available payload to 0 (pure handshake). After handshake, TCP options in data segments (timestamps: 10 bytes, SACK: 8–40 bytes) reduce the available payload per segment. A calculated 1460 bytes/segment payload may actually be 1430–1450 bytes with options. Use tcp_info to read the actual MSS in use rather than calculating from MTU alone.
4. Using TCP Urgent Data for IoT Priority Signaling
TCP Urgent (URG) flag and urgent pointer are deprecated and widely ignored by modern network stacks and firewalls. Many middle boxes strip or drop TCP segments with the URG flag set. IoT applications requiring priority data delivery must implement priority at the application layer: separate high-priority MQTT topic, priority queue in the application server, or separate TCP connections for priority vs background data. Never rely on TCP URG for any production IoT priority signaling.
8.11 What’s Next?
You have compared TCP and UDP fundamentals and can now evaluate the right transport protocol for an IoT scenario. Continue your learning with the topics below: