NetBurner 3.5.6
PDF Version
Network Protocols TCP and UDP

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

  • Establish connection (dial and answer)
  • Two-way conversation with confirmation ("Hello?" "Yes, I hear you")
  • Orderly exchange of information
  • Explicit termination ("Goodbye")

    UDP**: Like sending postcards

  • Write and send without confirmation
  • No guarantee of delivery
  • No confirmation the recipient received it
  • Messages may arrive out of order

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

  1. TCP provides reliability at the cost of overhead and connection management
  2. UDP provides speed and simplicity at the cost of reliability
  3. SNMP uses UDP to avoid connection overhead when monitoring many devices
  4. Custom protocols may choose UDP and implement their own reliability layer
  5. 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

Function Description
listen() Create listening socket
accept() Accept incoming connection
connect() Initiate outbound connection
read() Receive data
write() Send data
close() Close connection
select() Multiplex multiple connections
fdprintf() Formatted output to socket
writestring() Send string to socket

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