method
dgram.Socket.bindSync
The synchronous counterpart of socket.bind(). bind(2) is a local, non-blocking system call, so the bind is performed inline and the resolved address is returned immediately, including the operating-system-assigned ephemeral port when port is 0:
const dgram = require('node:dgram');
const socket = dgram.createSocket('udp4');
const address = socket.bindSync({ address: '0.0.0.0', port: 0 });
console.log(address); // e.g. { address: '0.0.0.0', family: 'IPv4', port: 53124 }A bind failure such as EADDRINUSE is thrown synchronously rather than emitted as an 'error' event. After bindSync() returns, socket.address() is valid synchronously and the 'listening' event is emitted on the next tick.
address must be a numeric IP literal; bindSync() never performs DNS resolution (asynchronous name resolution being the only genuinely blocking part of binding). Incoming datagrams continue to be delivered asynchronously via the 'message' event. bindSync() always binds the socket's own handle and does not participate in cluster handle sharing.
The bound address as returned by socket.address().