TCP vs UDP Comparison
This guide covers TCP and UDP network protocols in NetBurner applications, including practical implementation patterns and complete working examples. The first section is an overview and comparison of TCP vs UDP, followed by detailed discussions of each:
When designing network applications, choosing between TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) is a critical architectural decision. Each protocol serves different use cases based on reliability requirements, overhead tolerance, and application characteristics.
Protocol Characteristics
TCP (Transmission Control Protocol)
- Connection-oriented: Point-to-point stream-based protocol
- Reliable delivery: Guarantees packet delivery with acknowledgments
- Ordered transmission: Data arrives in the same sequence it was sent
- Error detection: Built-in error checking and retransmission
- Stream-based: Continuous data flow without message boundaries
UDP (User Datagram Protocol)
- Connectionless: Fire-and-forget datagram protocol
- Best-effort delivery: No delivery guarantees
- Unordered: Packets may arrive out of sequence
- Lightweight: Minimal overhead, no connection establishment
- Message-based: Each send operation creates one discrete datagram
Protocol Comparison Diagram
TCP Connection Flow UDP Transmission Flow
┌────────────┐ ┌────────────┐
│ Client │ │ Sender │
└─────┬──────┘ └─────┬──────┘
│ │
│ SYN │ Datagram 1
├────────> ├────────>
│ ┌────────────┐ │ ┌────────────┐
│ SYN-ACK │ Server │ │ │ Receiver │
<────────┤ └────────────┘ │ └────────────┘
│ │ │ Datagram 2
│ ACK │ ├────────>
├────────> │ │
│ │ │ Datagram 3
│ Data > │ ├────────>
├────────> │ │
│ < Ack │ │ (No acknowledgments)
<────────┤ │ │
│ │ v
│ FIN │
├────────> │
v v
Real-World Analogies
TCP**: Like a telephone call
Decision Factors
Choose TCP When:
- Reliability is critical: Financial transactions, control commands, configuration data
- Order matters: Sequential data like file transfers or protocol messages
- Connection state is useful: Authentication, session management
- Data integrity is paramount: Critical system operations
Choose UDP When:
- Speed is essential: Real-time applications, gaming, video streaming
- Overhead is prohibitive: High-frequency sensor data, telemetry
- Application handles reliability: Custom retry logic at application layer
- Broadcast/multicast needed: Network discovery, status updates
- Occasional loss is acceptable: Streaming media, periodic updates
Common Protocol Usage
Applications Using TCP:
- HTTP/HTTPS - Web traffic
- FTP - File transfers
- Telnet - Remote terminal
- SMTP - Email transmission
- SSH - Secure remote access
Applications Using UDP:
- DHCP - Network configuration
- BOOTP - Bootstrap protocol
- SNMP - Network monitoring
- DNS - Name resolution
- NTP - Time synchronization
- VoIP - Voice communications
Performance Considerations
TCP Overhead Example**:
Sending 10 A/D readings using TCP (single connection):
┌─────────────────────────────────────────┐
│ 9827312932342498273129323424982731293234│ <- Stream of digits
└─────────────────────────────────────────┘
Problem: Cannot identify individual readings without delimiters
UDP Datagram Example**:
Sending 10 A/D readings using UDP:
┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐
│ 9827 │ │ 3129 │ │ 3234 │ │ 2498 │ ...
└──────┘ └──────┘ └──────┘ └──────┘
Advantage: Each reading is a discrete packet
Key Takeaways
- TCP provides reliability at the cost of overhead and connection management
- UDP provides speed and simplicity at the cost of reliability
- SNMP uses UDP to avoid connection overhead when monitoring many devices
- Custom protocols may choose UDP and implement their own reliability layer
- Connection parameters: TCP requires 4-tuple (source IP, source port, dest IP, dest port)
Additional Resources
NetBurner Tools
- UDP Terminal (Windows): Test UDP communication from PC
- TcpServerWin.exe: Simple TCP server for testing
- MTTTY: Serial terminal for debug output
- nbdiscovery: Network device discovery
Example Locations
- TCP Examples:
\nburn\examples\TCP
- UDP Examples:
\nburn\examples\UDP
- Web Examples:
\nburn\examples\Web
- PC Tools:
\nburn\pctools
Related Documentation
- HTTP/Web Server Implementation
- Serial Communication Protocols
- MQTT Client/Server
- SSL/TLS Secure Connections
- Network Configuration APIs
Quick Reference
TCP Functions
UDP Class Methods
| Function | Description |
| RegisterUDPFifo() | Register FIFO for port |
| UDPPacket() | Constructor (receives packet) |
| Validate() | Check packet validity |
| GetDataBuffer() | Get packet data pointer |
| GetDataSize() | Get data length |
| GetSourceAddress() | Get sender IP |
| SetSourcePort() | Set source port |
| SetDestinationPort() | Set destination port |
| AddData() | Add data to packet |
| Send() | Transmit packet |
UDP Socket Functions
| Function | Description |
| RegisterUDPFifo() | Register FIFO for port |
| UDPPacket() | Constructor (receives packet) |
| Validate() | Check packet validity |
| GetDataBuffer() | Get packet data pointer |
| GetDataSize() | Get data length |
| GetSourceAddress() | Get sender IP |
| SetSourcePort() | Set source port |
| SetDestinationPort() | Set destination port |
| AddData() | Add data to packet |
| Send() | Transmit packet |
| CreateRxUdpSocket() | Create receive socket |
| CreateTxUdpSocket() | Create transmit socket |
| recvfrom() | Receive datagram |
| sendto() | Send datagram |