node:dgram module functions
History
dgram.createSocket(options, callback?): dgram.Socket
Objectstring'udp4' or 'udp6'.
Required.booleantrue socket.bind() will reuse the
address, even if another process has already bound a socket on it, but
only one socket can receive the data.
Default: false.booleantrue socket.bind() will reuse the
port, even if another process has already bound a socket on it. Incoming
datagrams are distributed to listening sockets. The option is available
only on some platforms, such as Linux 3.9+, DragonFlyBSD 3.6+, FreeBSD 12.0+,
Solaris 11.4, and AIX 7.2.5+. On unsupported platforms, this option raises
an error when the socket is bound.
Default: false.booleanipv6Only to true will
disable dual-stack support, i.e., binding to address :: won't make
0.0.0.0 be bound. Default: false.numberSO_RCVBUF socket value.numberSO_SNDBUF socket value.Functiondns.lookup().
When the default is used, a literal IP address of the socket's family
resolves to itself without calling dns.lookup().AbortSignalnet.BlockListreceiveBlockList can be used for discarding
inbound datagram to specific IP addresses, IP ranges, or IP subnets. This does not
work if the server is behind a reverse proxy, NAT, etc. because the address
checked against the blocklist is the address of the proxy, or the one
specified by the NAT.net.BlockListsendBlockList can be used for disabling outbound
access to specific IP addresses, IP ranges, or IP subnets.Function'message' events. Optional.dgram.SocketCreates a dgram.Socket object. Once the socket is created, calling
socket.bind() will instruct the socket to begin listening for datagram
messages. When address and port are not passed to socket.bind() the
method will bind the socket to the "all interfaces" address on a random port
(it does the right thing for both udp4 and udp6 sockets). The bound address
and port can be retrieved using socket.address().address and
socket.address().port.
If the signal option is enabled, calling .abort() on the corresponding
AbortController is similar to calling .close() on the socket:
const controller = new AbortController(); const { signal } = controller; const server = dgram.createSocket({ type: 'udp4', signal }); server.on('message', (msg, rinfo) => { console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`); }); // Later, when you want to close the server. controller.abort();
dgram.createSocket(type, callback?): dgram.Socket
string'udp4' or 'udp6'.Function'message' events.dgram.SocketCreates a dgram.Socket object of the specified type.
Once the socket is created, calling socket.bind() will instruct the
socket to begin listening for datagram messages. When address and port are
not passed to socket.bind() the method will bind the socket to the "all
interfaces" address on a random port (it does the right thing for both udp4
and udp6 sockets). The bound address and port can be retrieved using
socket.address().address and socket.address().port.