Sobes.tech
Junior — Middle

Can you explain the key differences between TCP and UDP protocols, and in which situations it is preferable to use each of them?

sobes.tech AI

Answer from AI

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two main transport protocols in networks.

Key differences:

  • Reliability: TCP ensures reliable data delivery — guarantees that all packets arrive and in the correct order. UDP does not guarantee delivery or order.
  • Connection establishment: TCP establishes a connection through a three-way handshake, UDP — without establishing a connection (connectionless).
  • Speed: UDP is faster because it does not spend time on acknowledgments and error checking.
  • Error control: TCP checks for errors and recovers lost data, UDP does not.

When to use:

  • TCP: for applications where reliability and order are important — web servers, email, file transfer.
  • UDP: for applications where speed and latency are critical, and some packet loss is acceptable — online gaming, video/audio streaming, VoIP.

Example of UDP usage in Python:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(b"Hello", ("example.com", 12345))
Can you explain the key differences between TCP and… - sobes.tech