Differences Between TCP and UDP

2020/02/10

Foreword

When it comes to the absolute top dogs of transport-layer protocols, it’s basically UDP and TCP. Based on their different characteristics, they both have a wide range of use cases. Sure, everyone complains about them, because looking at them today, they have some pretty big shortcomings (I only recently learned Google is pushing QUIC like crazy—worth checking out). But they’re still the backbone of networking.

image-20210320181043558

TCP

TCP (Transmission Control Protocol) provides a connection-oriented, reliable byte-stream service. In other words, before a client and server exchange data, they must first establish a TCP connection between them, and only then can they transmit data. It also provides features like timeout retransmission, duplicate data discarding, data checking, flow control, etc., ensuring data can be delivered from one end to the other.

  1. TCP has a lot of header fields. Besides the source and destination ports, it also includes reliability-related parts like sequence numbers and acknowledgment numbers to ensure ordering, as well as the window size, etc.
  2. TCP is connection-based. If you want to send messages using TCP, you must establish a connection—what everyone calls the three-way handshake. I’ll write another post later about my understanding of the three-way handshake.
  3. TCP also provides reliable transmission, including the ACK mechanism, timeout retransmission, congestion control, and flow control.
  4. TCP is byte-stream based. That is, TCP splits data and encapsulates it with headers. As long as the connection stays alive, there’s effectively no file-size limit, and it also has buffers to help ensure send/receive quality.

UDP

UDP (User Data Protocol, User Datagram Protocol) is a simple datagram-oriented transport-layer protocol. It does not provide reliability; it simply sends the datagrams passed from the application to the IP layer, but it cannot guarantee they will reach the destination. Since UDP does not need to establish a connection between the client and server before transmitting datagrams, and it has no mechanisms like timeout retransmission, it’s very fast.

  1. UDP doesn’t require a connection, so it’s very fast to “connect”—you can send packets as long as you have the peer IP and the source port (assuming the firewall doesn’t block it).
  2. UDP is best-effort delivery, meaning it does not guarantee reliable delivery. If a packet is lost, it’s lost—UDP doesn’t care.
  3. UDP supports one-to-one, one-to-many, and many-to-many.
  4. DUP transmission is datagram-oriented: whatever length of message the application hands to UDP, UDP sends it as-is—no splitting and no merging, and there is a size limit.

Similarities and Differences Between TCP and UDP

TCP UDP is connectionless and provides best-effort data transmission, without guaranteeing reliability
TCP is byte-stream oriented UDP is message-oriented (datagram-oriented)
Whatever length of message the application process hands to UDP UDP sends it as-is, sending one message at a time
TCP is connection-oriented and provides reliable data transmission UDP is connectionless and provides best-effort data transmission, without guaranteeing reliability
TCP data transmission is slow UDP data transmission is fast
TCP has congestion control UDP has no congestion control, so when the network is congested it won’t reduce the source host’s sending rate (very effective for real-time apps like live streaming, real-time video conferencing, etc.)
TCP can only do one-to-one communication (the endpoints of a TCP connection are sockets) UDP supports one-to-one, one-to-many, many-to-one, and many-to-many communication
TCP has a large header overhead: 20 bytes UDP is 8 bytes
TCP provides reliable full-duplex communication UDP is half-duplex: it can only transmit in one direction and is unreliable

All articles in this blog, unless otherwise stated, are licensed under @Oreoft . Please indicate the source when reprinting!

Table of Contents