Sobes.tech
Junior — Middle

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

sobes.tech AI

Answer from AI

UDP (User Datagram Protocol) is a transport protocol used for data transmission over a network. Unlike TCP, UDP does not establish a connection and does not guarantee packet delivery, making it faster but less reliable.

Main characteristics of UDP:

  • Connectionless.
  • Does not guarantee delivery, order, or data integrity.
  • Minimal delay and overhead.

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

In Node.js, the dgram module is used for working with UDP. Example of creating a UDP server:

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

server.on('message', (msg, rinfo) => {
  console.log(`Received message: ${msg} from ${rinfo.address}:${rinfo.port}`);
});

server.bind(41234);

Thus, UDP is a simple and fast protocol for message transmission without delivery guarantees.