Sobes.tech
Junior

What are the main differences between TCP and UDP protocols?

sobes.tech AI

Answer from AI

Transport protocols TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) have key differences that determine their usage:

  • Connection-oriented: TCP is a connection-oriented protocol. Before data transfer, the client and server go through a "handshake" process that guarantees connection establishment. UDP is connectionless. Data is sent immediately without prior agreement.

  • Reliability: TCP ensures reliable data delivery. It guarantees that packets reach the recipient in the correct order and without loss, using acknowledgment mechanisms, retransmission, and flow control. UDP does not guarantee delivery, order, or absence of duplication.

  • Speed: UDP is faster than TCP because it lacks the overhead of connection establishment and reliability mechanisms.

  • Error checking: TCP includes error control and recovery mechanisms. UDP provides only basic checksum verification to detect errors in the header.

  • Flow and congestion control: TCP has built-in mechanisms for flow control (preventing receiver buffer overflow) and congestion control (regulating transmission speed to avoid network overload). UDP does not have these mechanisms.

  • Header size: TCP header is usually larger (20-60 bytes) due to additional fields for connection management and reliability. UDP header is smaller (8 bytes).

Feature TCP UDP
Connection-oriented Yes No
Reliability High (guaranteed delivery) Low (unreliable delivery)
Speed Lower Higher
Header size Larger (20-60 bytes) Smaller (8 bytes)
Flow control Yes No
Congestion control Yes No
Overload control
Examples HTTP(S), FTP, SSH, SMTP DNS, DHCP, RTP

Example of establishing a connection in TCP (three-way handshake):

// Client sends SYN (Synchronization) packet
// Server receives SYN and sends SYN-ACK (Synchronization-Acknowledgement)
// Client receives SYN-ACK and sends ACK (Acknowledgement)

Example of data transmission in UDP:

// Sender simply sends a data packet
// Receiver may or may not receive the packet at any time