TCP vs. UDP

Learn the difference between TCP and UDP

Transmission Control Protocol and User Datagram Protocol are two transport layer protocols that are widely used with Internet Protocol. A protocol is a set of procedures and rules that two computers follow to understand each other and exchange data. Although TCP is the most commonly used protocol of the two, UDP is more useful in some situations.

Overall Findings

TCP
  • Guarantees that data arrives as sent.

  • Error-checks streams of data.

  • A 20-byte header permits an optional 40 bytes of function data.

  • Slower than UDP.

  • Best for apps that require reliability.

UDP
  • No guarantee that data arrives.

  • No error-checking provided.

  • An 8-byte header allows only compulsory function data.

  • Faster than TCP.

  • Best for apps that require speed.

Both protocols send data over the internet in packets. Of the two, TCP is connection-oriented. After a connection is made, data travels bidirectionally. UDP is a simpler protocol that is connectionless.

TCP is more robust than UDP. It provides error-correction functions and high reliability. UDP is faster than TCP, primarily because it doesn't provide error-correction. Additionally, TCP handles flow control, whereas UDP does not have an option for flow control.

Best for Reliability: TCP

TCP
  • Guaranteed delivery of data.

  • Tracks data packets.

  • Checks packets for errors.

  • Flow control.

  • Headers of 20 bytes with an option for more.

UDP
  • No guarantee of delivery.

  • Packets can be lost.

  • No flow control.

  • Headers are limited to 8 bytes.

TCP guarantees data delivery to the receiver. If any data is lost, TCP takes steps to recover the lost data and resends it. To do this, TCP tracks packets of data, and it checks the packets for errors.

UDP doesn't guarantee the delivery of data. In addition, UDP packets can be lost in transit or become corrupt.

Both TCP and UDP use headers as part of packaging the message data for transfer over network connections. Because TCP is the more robust of the two protocols, its header is larger at 20 bytes with an option for additional data, while UDP headers are limited to 8 bytes in size.

Best for Speed: UDP

TCP
  • Slow because it performs many functions.

UDP
  • Fast because it provides limited functions.

Although TCP is reliable, it is slower than UDP, mainly because it carries out more functions. End users who require the fastest speed possible, such as gamers and people who work with video, benefit from UDP.

Best for Usage: Depends on the Use

TCP
  • Best for web, file transfer, email, and secure shell.

UDP
  • Best for VPNs, streaming video, VoIP, live broadcasts, and online gaming.

Because TCP is reliable, it is best in situations that require high reliability but don't demand speed, such as the web, email, and FTP.

UDP's speed makes it most suitable for online gaming, live broadcasts, VPN tunneling, and streaming videos.

TCP Header Format

Each TCP header has 10 required fields totaling 20 bytes (160 bits) in size. It can optionally include an additional data field up to 40 bytes in size.

Transmission Control Protocol (TCP) Header
 Lifewire

8 bits equal 1 byte.

TCP headers appear in the following sequence, beginning with the source and destination communication endpoints:

  • Source TCP port number (2 bytes or 16 bits): The source TCP port number represents the sending device.
  • Destination TCP port number (2 bytes or 16 bits): The destination TCP port number is the communication endpoint for the receiving device.
  • Sequence number (4 bytes or 32 bits): Message senders use sequence numbers to mark the ordering of a group of messages.
  • Acknowledgment number (4 bytes or 32 bits): Both senders and receivers use the acknowledgment numbers field to communicate the sequence numbers of messages that are either recently received or expected to be sent.
  • TCP data offset (4 bits): The data offset field stores the total size of a TCP header in multiples of four bytes. A header not using the optional TCP field has a data offset of 5 (representing 20 bytes), while a header using the maximum-sized optional field has a data offset of 15 (representing 60 bytes).
  • Reserved data (3 bits): Reserved data in TCP headers always has a value of zero. This field aligns the total header size as a multiple of four bytes, which is important for the efficiency of computer data processing.
  • Control flags (up to 9 bits): TCP uses a set of six standard and three extended control flags—each an individual bit representing On or Off—to manage data flow in specific situations.
  • Window size (2 bytes or 16 bits): TCP senders use a number, called window size, to regulate how much data they send to a receiver before requiring an acknowledgment in return. If the window size is too small, network data transfer is unnecessarily slow. If the window size is too large, the network link may become saturated, or the receiver may not be able to process incoming data quickly enough, resulting in slow performance. Windowing algorithms built into the protocol dynamically calculate size values and use this field of TCP headers to coordinate changes between senders and receivers.
  • TCP checksum (2 bytes or 16 bits): The checksum value inside a TCP header is generated by the protocol sender as a mathematical technique to help the receiver detect messages that are corrupted or tampered with.
  • Urgent pointer (2 bytes or 16 bits): The urgent pointer field is often set to zero and ignored, but in conjunction with one of the control flags, it can be used as a data offset to mark a subset of a message as requiring priority processing.
  • TCP optional data (0 to 40 bytes): Usages of optional TCP data include support for special acknowledgment and window scaling algorithms.

UDP Header Format

Because UDP is limited in capability compared to TCP, its headers are smaller. A UDP header contains 8 bytes, divided into the following four required fields:

  • Source UDP port number (2 bytes): The source UDP port number represents the sending device.
  • Destination UDP port number (2 bytes): The destination UDP port number is the communication endpoint for the receiving device.
  • Length of data (2 bytes): The length field in UDP represents the total size of each datagram, including both header and data. This field ranges in value from a minimum of 8 bytes—the required header size—to sizes above 65,000 bytes.
  • UDP checksum (2 bytes): Similar to TCP, a UDP checksum allows receivers to cross-check incoming data for any corrupted bits of the message.

Final Verdict

There's a reason TCP is the most commonly used protocol. It is robust and reliable, and it guarantees data is received exactly as it was sent. Its error-checked stream of information may slow it down slightly, but in most cases, the delay isn't a deal-breaker.

The time when UDP shines brighter than TCP is when speed is critically important such as in the streaming of videos, VPNs, or online gaming.