Sobes.tech
Junior — Middle

Can you explain what the UDP protocol is and how it works in a network?

sobes.tech AI

Answer from AI

UDP (User Datagram Protocol) is a transport layer protocol that provides data transfer without establishing a connection (connectionless). Unlike TCP, UDP does not guarantee delivery, order, or integrity of packets, but it operates faster and with lower overhead.

How UDP works:

  • The sender forms datagrams (packets) and sends them to the specified IP address and port.
  • The receiver listens on a specific port and accepts packets as they arrive.
  • There are no acknowledgments of delivery, retransmissions, or flow control.

UDP is often used in applications where speed is crucial and some packet loss is acceptable, such as streaming video, voice communication, and online gaming.

Example in Python for sending and receiving UDP messages:

import socket

# Sender
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 10000)
message = b'Hello UDP'
sock.sendto(message, server_address)

# Receiver
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('localhost', 10000))
data, addr = sock.recvfrom(4096)
print('Received:', data.decode())