11cb0ef41Sopenharmony_ci# UDP/datagram sockets
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- name=dgram -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci<!-- source_link=lib/dgram.js -->
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciThe `node:dgram` module provides an implementation of UDP datagram sockets.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci```mjs
141cb0ef41Sopenharmony_ciimport dgram from 'node:dgram';
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciserver.on('error', (err) => {
191cb0ef41Sopenharmony_ci  console.error(`server error:\n${err.stack}`);
201cb0ef41Sopenharmony_ci  server.close();
211cb0ef41Sopenharmony_ci});
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciserver.on('message', (msg, rinfo) => {
241cb0ef41Sopenharmony_ci  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
251cb0ef41Sopenharmony_ci});
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciserver.on('listening', () => {
281cb0ef41Sopenharmony_ci  const address = server.address();
291cb0ef41Sopenharmony_ci  console.log(`server listening ${address.address}:${address.port}`);
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciserver.bind(41234);
331cb0ef41Sopenharmony_ci// Prints: server listening 0.0.0.0:41234
341cb0ef41Sopenharmony_ci```
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci```cjs
371cb0ef41Sopenharmony_ciconst dgram = require('node:dgram');
381cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciserver.on('error', (err) => {
411cb0ef41Sopenharmony_ci  console.error(`server error:\n${err.stack}`);
421cb0ef41Sopenharmony_ci  server.close();
431cb0ef41Sopenharmony_ci});
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciserver.on('message', (msg, rinfo) => {
461cb0ef41Sopenharmony_ci  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
471cb0ef41Sopenharmony_ci});
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciserver.on('listening', () => {
501cb0ef41Sopenharmony_ci  const address = server.address();
511cb0ef41Sopenharmony_ci  console.log(`server listening ${address.address}:${address.port}`);
521cb0ef41Sopenharmony_ci});
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciserver.bind(41234);
551cb0ef41Sopenharmony_ci// Prints: server listening 0.0.0.0:41234
561cb0ef41Sopenharmony_ci```
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci## Class: `dgram.Socket`
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci<!-- YAML
611cb0ef41Sopenharmony_ciadded: v0.1.99
621cb0ef41Sopenharmony_ci-->
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci* Extends: {EventEmitter}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciEncapsulates the datagram functionality.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciNew instances of `dgram.Socket` are created using [`dgram.createSocket()`][].
691cb0ef41Sopenharmony_ciThe `new` keyword is not to be used to create `dgram.Socket` instances.
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci### Event: `'close'`
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci<!-- YAML
741cb0ef41Sopenharmony_ciadded: v0.1.99
751cb0ef41Sopenharmony_ci-->
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciThe `'close'` event is emitted after a socket is closed with [`close()`][].
781cb0ef41Sopenharmony_ciOnce triggered, no new `'message'` events will be emitted on this socket.
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci### Event: `'connect'`
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci<!-- YAML
831cb0ef41Sopenharmony_ciadded: v12.0.0
841cb0ef41Sopenharmony_ci-->
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciThe `'connect'` event is emitted after a socket is associated to a remote
871cb0ef41Sopenharmony_ciaddress as a result of a successful [`connect()`][] call.
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci### Event: `'error'`
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci<!-- YAML
921cb0ef41Sopenharmony_ciadded: v0.1.99
931cb0ef41Sopenharmony_ci-->
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci* `exception` {Error}
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciThe `'error'` event is emitted whenever any error occurs. The event handler
981cb0ef41Sopenharmony_cifunction is passed a single `Error` object.
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci### Event: `'listening'`
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci<!-- YAML
1031cb0ef41Sopenharmony_ciadded: v0.1.99
1041cb0ef41Sopenharmony_ci-->
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciThe `'listening'` event is emitted once the `dgram.Socket` is addressable and
1071cb0ef41Sopenharmony_cican receive data. This happens either explicitly with `socket.bind()` or
1081cb0ef41Sopenharmony_ciimplicitly the first time data is sent using `socket.send()`.
1091cb0ef41Sopenharmony_ciUntil the `dgram.Socket` is listening, the underlying system resources do not
1101cb0ef41Sopenharmony_ciexist and calls such as `socket.address()` and `socket.setTTL()` will fail.
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci### Event: `'message'`
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci<!-- YAML
1151cb0ef41Sopenharmony_ciadded: v0.1.99
1161cb0ef41Sopenharmony_cichanges:
1171cb0ef41Sopenharmony_ci  - version: v18.4.0
1181cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/43054
1191cb0ef41Sopenharmony_ci    description: The `family` property now returns a string instead of a number.
1201cb0ef41Sopenharmony_ci  - version: v18.0.0
1211cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41431
1221cb0ef41Sopenharmony_ci    description: The `family` property now returns a number instead of a string.
1231cb0ef41Sopenharmony_ci-->
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciThe `'message'` event is emitted when a new datagram is available on a socket.
1261cb0ef41Sopenharmony_ciThe event handler function is passed two arguments: `msg` and `rinfo`.
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci* `msg` {Buffer} The message.
1291cb0ef41Sopenharmony_ci* `rinfo` {Object} Remote address information.
1301cb0ef41Sopenharmony_ci  * `address` {string} The sender address.
1311cb0ef41Sopenharmony_ci  * `family` {string} The address family (`'IPv4'` or `'IPv6'`).
1321cb0ef41Sopenharmony_ci  * `port` {number} The sender port.
1331cb0ef41Sopenharmony_ci  * `size` {number} The message size.
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ciIf the source address of the incoming packet is an IPv6 link-local
1361cb0ef41Sopenharmony_ciaddress, the interface name is added to the `address`. For
1371cb0ef41Sopenharmony_ciexample, a packet received on the `en0` interface might have the
1381cb0ef41Sopenharmony_ciaddress field set to `'fe80::2618:1234:ab11:3b9c%en0'`, where `'%en0'`
1391cb0ef41Sopenharmony_ciis the interface name as a zone ID suffix.
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci### `socket.addMembership(multicastAddress[, multicastInterface])`
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci<!-- YAML
1441cb0ef41Sopenharmony_ciadded: v0.6.9
1451cb0ef41Sopenharmony_ci-->
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci* `multicastAddress` {string}
1481cb0ef41Sopenharmony_ci* `multicastInterface` {string}
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ciTells the kernel to join a multicast group at the given `multicastAddress` and
1511cb0ef41Sopenharmony_ci`multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. If the
1521cb0ef41Sopenharmony_ci`multicastInterface` argument is not specified, the operating system will choose
1531cb0ef41Sopenharmony_cione interface and will add membership to it. To add membership to every
1541cb0ef41Sopenharmony_ciavailable interface, call `addMembership` multiple times, once per interface.
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ciWhen called on an unbound socket, this method will implicitly bind to a random
1571cb0ef41Sopenharmony_ciport, listening on all interfaces.
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ciWhen sharing a UDP socket across multiple `cluster` workers, the
1601cb0ef41Sopenharmony_ci`socket.addMembership()` function must be called only once or an
1611cb0ef41Sopenharmony_ci`EADDRINUSE` error will occur:
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci```mjs
1641cb0ef41Sopenharmony_ciimport cluster from 'node:cluster';
1651cb0ef41Sopenharmony_ciimport dgram from 'node:dgram';
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
1681cb0ef41Sopenharmony_ci  cluster.fork(); // Works ok.
1691cb0ef41Sopenharmony_ci  cluster.fork(); // Fails with EADDRINUSE.
1701cb0ef41Sopenharmony_ci} else {
1711cb0ef41Sopenharmony_ci  const s = dgram.createSocket('udp4');
1721cb0ef41Sopenharmony_ci  s.bind(1234, () => {
1731cb0ef41Sopenharmony_ci    s.addMembership('224.0.0.114');
1741cb0ef41Sopenharmony_ci  });
1751cb0ef41Sopenharmony_ci}
1761cb0ef41Sopenharmony_ci```
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci```cjs
1791cb0ef41Sopenharmony_ciconst cluster = require('node:cluster');
1801cb0ef41Sopenharmony_ciconst dgram = require('node:dgram');
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
1831cb0ef41Sopenharmony_ci  cluster.fork(); // Works ok.
1841cb0ef41Sopenharmony_ci  cluster.fork(); // Fails with EADDRINUSE.
1851cb0ef41Sopenharmony_ci} else {
1861cb0ef41Sopenharmony_ci  const s = dgram.createSocket('udp4');
1871cb0ef41Sopenharmony_ci  s.bind(1234, () => {
1881cb0ef41Sopenharmony_ci    s.addMembership('224.0.0.114');
1891cb0ef41Sopenharmony_ci  });
1901cb0ef41Sopenharmony_ci}
1911cb0ef41Sopenharmony_ci```
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci### `socket.addSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])`
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci<!-- YAML
1961cb0ef41Sopenharmony_ciadded:
1971cb0ef41Sopenharmony_ci - v13.1.0
1981cb0ef41Sopenharmony_ci - v12.16.0
1991cb0ef41Sopenharmony_ci-->
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci* `sourceAddress` {string}
2021cb0ef41Sopenharmony_ci* `groupAddress` {string}
2031cb0ef41Sopenharmony_ci* `multicastInterface` {string}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ciTells the kernel to join a source-specific multicast channel at the given
2061cb0ef41Sopenharmony_ci`sourceAddress` and `groupAddress`, using the `multicastInterface` with the
2071cb0ef41Sopenharmony_ci`IP_ADD_SOURCE_MEMBERSHIP` socket option. If the `multicastInterface` argument
2081cb0ef41Sopenharmony_ciis not specified, the operating system will choose one interface and will add
2091cb0ef41Sopenharmony_cimembership to it. To add membership to every available interface, call
2101cb0ef41Sopenharmony_ci`socket.addSourceSpecificMembership()` multiple times, once per interface.
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ciWhen called on an unbound socket, this method will implicitly bind to a random
2131cb0ef41Sopenharmony_ciport, listening on all interfaces.
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci### `socket.address()`
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci<!-- YAML
2181cb0ef41Sopenharmony_ciadded: v0.1.99
2191cb0ef41Sopenharmony_ci-->
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci* Returns: {Object}
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ciReturns an object containing the address information for a socket.
2241cb0ef41Sopenharmony_ciFor UDP sockets, this object will contain `address`, `family`, and `port`
2251cb0ef41Sopenharmony_ciproperties.
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ciThis method throws `EBADF` if called on an unbound socket.
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci### `socket.bind([port][, address][, callback])`
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci<!-- YAML
2321cb0ef41Sopenharmony_ciadded: v0.1.99
2331cb0ef41Sopenharmony_cichanges:
2341cb0ef41Sopenharmony_ci  - version: v0.9.1
2351cb0ef41Sopenharmony_ci    commit: 332fea5ac1816e498030109c4211bca24a7fa667
2361cb0ef41Sopenharmony_ci    description: The method was changed to an asynchronous execution model.
2371cb0ef41Sopenharmony_ci                 Legacy code would need to be changed to pass a callback
2381cb0ef41Sopenharmony_ci                 function to the method call.
2391cb0ef41Sopenharmony_ci-->
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci* `port` {integer}
2421cb0ef41Sopenharmony_ci* `address` {string}
2431cb0ef41Sopenharmony_ci* `callback` {Function} with no parameters. Called when binding is complete.
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ciFor UDP sockets, causes the `dgram.Socket` to listen for datagram
2461cb0ef41Sopenharmony_cimessages on a named `port` and optional `address`. If `port` is not
2471cb0ef41Sopenharmony_cispecified or is `0`, the operating system will attempt to bind to a
2481cb0ef41Sopenharmony_cirandom port. If `address` is not specified, the operating system will
2491cb0ef41Sopenharmony_ciattempt to listen on all addresses. Once binding is complete, a
2501cb0ef41Sopenharmony_ci`'listening'` event is emitted and the optional `callback` function is
2511cb0ef41Sopenharmony_cicalled.
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciSpecifying both a `'listening'` event listener and passing a
2541cb0ef41Sopenharmony_ci`callback` to the `socket.bind()` method is not harmful but not very
2551cb0ef41Sopenharmony_ciuseful.
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ciA bound datagram socket keeps the Node.js process running to receive
2581cb0ef41Sopenharmony_cidatagram messages.
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ciIf binding fails, an `'error'` event is generated. In rare case (e.g.
2611cb0ef41Sopenharmony_ciattempting to bind with a closed socket), an [`Error`][] may be thrown.
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ciExample of a UDP server listening on port 41234:
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci```mjs
2661cb0ef41Sopenharmony_ciimport dgram from 'node:dgram';
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ciserver.on('error', (err) => {
2711cb0ef41Sopenharmony_ci  console.error(`server error:\n${err.stack}`);
2721cb0ef41Sopenharmony_ci  server.close();
2731cb0ef41Sopenharmony_ci});
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ciserver.on('message', (msg, rinfo) => {
2761cb0ef41Sopenharmony_ci  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
2771cb0ef41Sopenharmony_ci});
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ciserver.on('listening', () => {
2801cb0ef41Sopenharmony_ci  const address = server.address();
2811cb0ef41Sopenharmony_ci  console.log(`server listening ${address.address}:${address.port}`);
2821cb0ef41Sopenharmony_ci});
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ciserver.bind(41234);
2851cb0ef41Sopenharmony_ci// Prints: server listening 0.0.0.0:41234
2861cb0ef41Sopenharmony_ci```
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci```cjs
2891cb0ef41Sopenharmony_ciconst dgram = require('node:dgram');
2901cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ciserver.on('error', (err) => {
2931cb0ef41Sopenharmony_ci  console.error(`server error:\n${err.stack}`);
2941cb0ef41Sopenharmony_ci  server.close();
2951cb0ef41Sopenharmony_ci});
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ciserver.on('message', (msg, rinfo) => {
2981cb0ef41Sopenharmony_ci  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
2991cb0ef41Sopenharmony_ci});
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciserver.on('listening', () => {
3021cb0ef41Sopenharmony_ci  const address = server.address();
3031cb0ef41Sopenharmony_ci  console.log(`server listening ${address.address}:${address.port}`);
3041cb0ef41Sopenharmony_ci});
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ciserver.bind(41234);
3071cb0ef41Sopenharmony_ci// Prints: server listening 0.0.0.0:41234
3081cb0ef41Sopenharmony_ci```
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci### `socket.bind(options[, callback])`
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci<!-- YAML
3131cb0ef41Sopenharmony_ciadded: v0.11.14
3141cb0ef41Sopenharmony_ci-->
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ci* `options` {Object} Required. Supports the following properties:
3171cb0ef41Sopenharmony_ci  * `port` {integer}
3181cb0ef41Sopenharmony_ci  * `address` {string}
3191cb0ef41Sopenharmony_ci  * `exclusive` {boolean}
3201cb0ef41Sopenharmony_ci  * `fd` {integer}
3211cb0ef41Sopenharmony_ci* `callback` {Function}
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ciFor UDP sockets, causes the `dgram.Socket` to listen for datagram
3241cb0ef41Sopenharmony_cimessages on a named `port` and optional `address` that are passed as
3251cb0ef41Sopenharmony_ciproperties of an `options` object passed as the first argument. If
3261cb0ef41Sopenharmony_ci`port` is not specified or is `0`, the operating system will attempt
3271cb0ef41Sopenharmony_cito bind to a random port. If `address` is not specified, the operating
3281cb0ef41Sopenharmony_cisystem will attempt to listen on all addresses. Once binding is
3291cb0ef41Sopenharmony_cicomplete, a `'listening'` event is emitted and the optional `callback`
3301cb0ef41Sopenharmony_cifunction is called.
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ciThe `options` object may contain a `fd` property. When a `fd` greater
3331cb0ef41Sopenharmony_cithan `0` is set, it will wrap around an existing socket with the given
3341cb0ef41Sopenharmony_cifile descriptor. In this case, the properties of `port` and `address`
3351cb0ef41Sopenharmony_ciwill be ignored.
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ciSpecifying both a `'listening'` event listener and passing a
3381cb0ef41Sopenharmony_ci`callback` to the `socket.bind()` method is not harmful but not very
3391cb0ef41Sopenharmony_ciuseful.
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ciThe `options` object may contain an additional `exclusive` property that is
3421cb0ef41Sopenharmony_ciused when using `dgram.Socket` objects with the [`cluster`][] module. When
3431cb0ef41Sopenharmony_ci`exclusive` is set to `false` (the default), cluster workers will use the same
3441cb0ef41Sopenharmony_ciunderlying socket handle allowing connection handling duties to be shared.
3451cb0ef41Sopenharmony_ciWhen `exclusive` is `true`, however, the handle is not shared and attempted
3461cb0ef41Sopenharmony_ciport sharing results in an error.
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ciA bound datagram socket keeps the Node.js process running to receive
3491cb0ef41Sopenharmony_cidatagram messages.
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ciIf binding fails, an `'error'` event is generated. In rare case (e.g.
3521cb0ef41Sopenharmony_ciattempting to bind with a closed socket), an [`Error`][] may be thrown.
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ciAn example socket listening on an exclusive port is shown below.
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci```js
3571cb0ef41Sopenharmony_cisocket.bind({
3581cb0ef41Sopenharmony_ci  address: 'localhost',
3591cb0ef41Sopenharmony_ci  port: 8000,
3601cb0ef41Sopenharmony_ci  exclusive: true,
3611cb0ef41Sopenharmony_ci});
3621cb0ef41Sopenharmony_ci```
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ci### `socket.close([callback])`
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci<!-- YAML
3671cb0ef41Sopenharmony_ciadded: v0.1.99
3681cb0ef41Sopenharmony_ci-->
3691cb0ef41Sopenharmony_ci
3701cb0ef41Sopenharmony_ci* `callback` {Function} Called when the socket has been closed.
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ciClose the underlying socket and stop listening for data on it. If a callback is
3731cb0ef41Sopenharmony_ciprovided, it is added as a listener for the [`'close'`][] event.
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci### `socket[Symbol.asyncDispose]()`
3761cb0ef41Sopenharmony_ci
3771cb0ef41Sopenharmony_ci<!-- YAML
3781cb0ef41Sopenharmony_ciadded: v18.18.0
3791cb0ef41Sopenharmony_ci-->
3801cb0ef41Sopenharmony_ci
3811cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ciCalls [`socket.close()`][] and returns a promise that fulfills when the
3841cb0ef41Sopenharmony_cisocket has closed.
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ci### `socket.connect(port[, address][, callback])`
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ci<!-- YAML
3891cb0ef41Sopenharmony_ciadded: v12.0.0
3901cb0ef41Sopenharmony_ci-->
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci* `port` {integer}
3931cb0ef41Sopenharmony_ci* `address` {string}
3941cb0ef41Sopenharmony_ci* `callback` {Function} Called when the connection is completed or on error.
3951cb0ef41Sopenharmony_ci
3961cb0ef41Sopenharmony_ciAssociates the `dgram.Socket` to a remote address and port. Every
3971cb0ef41Sopenharmony_cimessage sent by this handle is automatically sent to that destination. Also,
3981cb0ef41Sopenharmony_cithe socket will only receive messages from that remote peer.
3991cb0ef41Sopenharmony_ciTrying to call `connect()` on an already connected socket will result
4001cb0ef41Sopenharmony_ciin an [`ERR_SOCKET_DGRAM_IS_CONNECTED`][] exception. If `address` is not
4011cb0ef41Sopenharmony_ciprovided, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` (for `udp6` sockets)
4021cb0ef41Sopenharmony_ciwill be used by default. Once the connection is complete, a `'connect'` event
4031cb0ef41Sopenharmony_ciis emitted and the optional `callback` function is called. In case of failure,
4041cb0ef41Sopenharmony_cithe `callback` is called or, failing this, an `'error'` event is emitted.
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci### `socket.disconnect()`
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci<!-- YAML
4091cb0ef41Sopenharmony_ciadded: v12.0.0
4101cb0ef41Sopenharmony_ci-->
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ciA synchronous function that disassociates a connected `dgram.Socket` from
4131cb0ef41Sopenharmony_ciits remote address. Trying to call `disconnect()` on an unbound or already
4141cb0ef41Sopenharmony_cidisconnected socket will result in an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`][]
4151cb0ef41Sopenharmony_ciexception.
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci### `socket.dropMembership(multicastAddress[, multicastInterface])`
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ci<!-- YAML
4201cb0ef41Sopenharmony_ciadded: v0.6.9
4211cb0ef41Sopenharmony_ci-->
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci* `multicastAddress` {string}
4241cb0ef41Sopenharmony_ci* `multicastInterface` {string}
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ciInstructs the kernel to leave a multicast group at `multicastAddress` using the
4271cb0ef41Sopenharmony_ci`IP_DROP_MEMBERSHIP` socket option. This method is automatically called by the
4281cb0ef41Sopenharmony_cikernel when the socket is closed or the process terminates, so most apps will
4291cb0ef41Sopenharmony_cinever have reason to call this.
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ciIf `multicastInterface` is not specified, the operating system will attempt to
4321cb0ef41Sopenharmony_cidrop membership on all valid interfaces.
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ci### `socket.dropSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])`
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci<!-- YAML
4371cb0ef41Sopenharmony_ciadded:
4381cb0ef41Sopenharmony_ci - v13.1.0
4391cb0ef41Sopenharmony_ci - v12.16.0
4401cb0ef41Sopenharmony_ci-->
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ci* `sourceAddress` {string}
4431cb0ef41Sopenharmony_ci* `groupAddress` {string}
4441cb0ef41Sopenharmony_ci* `multicastInterface` {string}
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_ciInstructs the kernel to leave a source-specific multicast channel at the given
4471cb0ef41Sopenharmony_ci`sourceAddress` and `groupAddress` using the `IP_DROP_SOURCE_MEMBERSHIP`
4481cb0ef41Sopenharmony_cisocket option. This method is automatically called by the kernel when the
4491cb0ef41Sopenharmony_cisocket is closed or the process terminates, so most apps will never have
4501cb0ef41Sopenharmony_cireason to call this.
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ciIf `multicastInterface` is not specified, the operating system will attempt to
4531cb0ef41Sopenharmony_cidrop membership on all valid interfaces.
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ci### `socket.getRecvBufferSize()`
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_ci<!-- YAML
4581cb0ef41Sopenharmony_ciadded: v8.7.0
4591cb0ef41Sopenharmony_ci-->
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_ci* Returns: {number} the `SO_RCVBUF` socket receive buffer size in bytes.
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_ciThis method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci### `socket.getSendBufferSize()`
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci<!-- YAML
4681cb0ef41Sopenharmony_ciadded: v8.7.0
4691cb0ef41Sopenharmony_ci-->
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_ci* Returns: {number} the `SO_SNDBUF` socket send buffer size in bytes.
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ciThis method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci### `socket.getSendQueueSize()`
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_ci<!-- YAML
4781cb0ef41Sopenharmony_ciadded: v18.8.0
4791cb0ef41Sopenharmony_ci-->
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci* Returns: {number} Number of bytes queued for sending.
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci### `socket.getSendQueueCount()`
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_ci<!-- YAML
4861cb0ef41Sopenharmony_ciadded: v18.8.0
4871cb0ef41Sopenharmony_ci-->
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ci* Returns: {number} Number of send requests currently in the queue awaiting
4901cb0ef41Sopenharmony_ci  to be processed.
4911cb0ef41Sopenharmony_ci
4921cb0ef41Sopenharmony_ci### `socket.ref()`
4931cb0ef41Sopenharmony_ci
4941cb0ef41Sopenharmony_ci<!-- YAML
4951cb0ef41Sopenharmony_ciadded: v0.9.1
4961cb0ef41Sopenharmony_ci-->
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci* Returns: {dgram.Socket}
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ciBy default, binding a socket will cause it to block the Node.js process from
5011cb0ef41Sopenharmony_ciexiting as long as the socket is open. The `socket.unref()` method can be used
5021cb0ef41Sopenharmony_cito exclude the socket from the reference counting that keeps the Node.js
5031cb0ef41Sopenharmony_ciprocess active. The `socket.ref()` method adds the socket back to the reference
5041cb0ef41Sopenharmony_cicounting and restores the default behavior.
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ciCalling `socket.ref()` multiples times will have no additional effect.
5071cb0ef41Sopenharmony_ci
5081cb0ef41Sopenharmony_ciThe `socket.ref()` method returns a reference to the socket so calls can be
5091cb0ef41Sopenharmony_cichained.
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci### `socket.remoteAddress()`
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci<!-- YAML
5141cb0ef41Sopenharmony_ciadded: v12.0.0
5151cb0ef41Sopenharmony_ci-->
5161cb0ef41Sopenharmony_ci
5171cb0ef41Sopenharmony_ci* Returns: {Object}
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ciReturns an object containing the `address`, `family`, and `port` of the remote
5201cb0ef41Sopenharmony_ciendpoint. This method throws an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`][] exception
5211cb0ef41Sopenharmony_ciif the socket is not connected.
5221cb0ef41Sopenharmony_ci
5231cb0ef41Sopenharmony_ci### `socket.send(msg[, offset, length][, port][, address][, callback])`
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_ci<!-- YAML
5261cb0ef41Sopenharmony_ciadded: v0.1.99
5271cb0ef41Sopenharmony_cichanges:
5281cb0ef41Sopenharmony_ci  - version: v17.0.0
5291cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/39190
5301cb0ef41Sopenharmony_ci    description: The `address` parameter now only accepts a `string`, `null`
5311cb0ef41Sopenharmony_ci                 or `undefined`.
5321cb0ef41Sopenharmony_ci  - version:
5331cb0ef41Sopenharmony_ci    - v14.5.0
5341cb0ef41Sopenharmony_ci    - v12.19.0
5351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22413
5361cb0ef41Sopenharmony_ci    description: The `msg` parameter can now be any `TypedArray` or `DataView`.
5371cb0ef41Sopenharmony_ci  - version: v12.0.0
5381cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/26871
5391cb0ef41Sopenharmony_ci    description: Added support for sending data on connected sockets.
5401cb0ef41Sopenharmony_ci  - version: v8.0.0
5411cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/11985
5421cb0ef41Sopenharmony_ci    description: The `msg` parameter can be an `Uint8Array` now.
5431cb0ef41Sopenharmony_ci  - version: v8.0.0
5441cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10473
5451cb0ef41Sopenharmony_ci    description: The `address` parameter is always optional now.
5461cb0ef41Sopenharmony_ci  - version: v6.0.0
5471cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5929
5481cb0ef41Sopenharmony_ci    description: On success, `callback` will now be called with an `error`
5491cb0ef41Sopenharmony_ci                 argument of `null` rather than `0`.
5501cb0ef41Sopenharmony_ci  - version: v5.7.0
5511cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/4374
5521cb0ef41Sopenharmony_ci    description: The `msg` parameter can be an array now. Also, the `offset`
5531cb0ef41Sopenharmony_ci                 and `length` parameters are optional now.
5541cb0ef41Sopenharmony_ci-->
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ci* `msg` {Buffer|TypedArray|DataView|string|Array} Message to be sent.
5571cb0ef41Sopenharmony_ci* `offset` {integer} Offset in the buffer where the message starts.
5581cb0ef41Sopenharmony_ci* `length` {integer} Number of bytes in the message.
5591cb0ef41Sopenharmony_ci* `port` {integer} Destination port.
5601cb0ef41Sopenharmony_ci* `address` {string} Destination host name or IP address.
5611cb0ef41Sopenharmony_ci* `callback` {Function} Called when the message has been sent.
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ciBroadcasts a datagram on the socket.
5641cb0ef41Sopenharmony_ciFor connectionless sockets, the destination `port` and `address` must be
5651cb0ef41Sopenharmony_cispecified. Connected sockets, on the other hand, will use their associated
5661cb0ef41Sopenharmony_ciremote endpoint, so the `port` and `address` arguments must not be set.
5671cb0ef41Sopenharmony_ci
5681cb0ef41Sopenharmony_ciThe `msg` argument contains the message to be sent.
5691cb0ef41Sopenharmony_ciDepending on its type, different behavior can apply. If `msg` is a `Buffer`,
5701cb0ef41Sopenharmony_ciany `TypedArray` or a `DataView`,
5711cb0ef41Sopenharmony_cithe `offset` and `length` specify the offset within the `Buffer` where the
5721cb0ef41Sopenharmony_cimessage begins and the number of bytes in the message, respectively.
5731cb0ef41Sopenharmony_ciIf `msg` is a `String`, then it is automatically converted to a `Buffer`
5741cb0ef41Sopenharmony_ciwith `'utf8'` encoding. With messages that
5751cb0ef41Sopenharmony_cicontain multi-byte characters, `offset` and `length` will be calculated with
5761cb0ef41Sopenharmony_cirespect to [byte length][] and not the character position.
5771cb0ef41Sopenharmony_ciIf `msg` is an array, `offset` and `length` must not be specified.
5781cb0ef41Sopenharmony_ci
5791cb0ef41Sopenharmony_ciThe `address` argument is a string. If the value of `address` is a host name,
5801cb0ef41Sopenharmony_ciDNS will be used to resolve the address of the host. If `address` is not
5811cb0ef41Sopenharmony_ciprovided or otherwise nullish, `'127.0.0.1'` (for `udp4` sockets) or `'::1'`
5821cb0ef41Sopenharmony_ci(for `udp6` sockets) will be used by default.
5831cb0ef41Sopenharmony_ci
5841cb0ef41Sopenharmony_ciIf the socket has not been previously bound with a call to `bind`, the socket
5851cb0ef41Sopenharmony_ciis assigned a random port number and is bound to the "all interfaces" address
5861cb0ef41Sopenharmony_ci(`'0.0.0.0'` for `udp4` sockets, `'::0'` for `udp6` sockets.)
5871cb0ef41Sopenharmony_ci
5881cb0ef41Sopenharmony_ciAn optional `callback` function may be specified to as a way of reporting
5891cb0ef41Sopenharmony_ciDNS errors or for determining when it is safe to reuse the `buf` object.
5901cb0ef41Sopenharmony_ciDNS lookups delay the time to send for at least one tick of the
5911cb0ef41Sopenharmony_ciNode.js event loop.
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ciThe only way to know for sure that the datagram has been sent is by using a
5941cb0ef41Sopenharmony_ci`callback`. If an error occurs and a `callback` is given, the error will be
5951cb0ef41Sopenharmony_cipassed as the first argument to the `callback`. If a `callback` is not given,
5961cb0ef41Sopenharmony_cithe error is emitted as an `'error'` event on the `socket` object.
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ciOffset and length are optional but both _must_ be set if either are used.
5991cb0ef41Sopenharmony_ciThey are supported only when the first argument is a `Buffer`, a `TypedArray`,
6001cb0ef41Sopenharmony_cior a `DataView`.
6011cb0ef41Sopenharmony_ci
6021cb0ef41Sopenharmony_ciThis method throws [`ERR_SOCKET_BAD_PORT`][] if called on an unbound socket.
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ciExample of sending a UDP packet to a port on `localhost`;
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ci```mjs
6071cb0ef41Sopenharmony_ciimport dgram from 'node:dgram';
6081cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
6091cb0ef41Sopenharmony_ci
6101cb0ef41Sopenharmony_ciconst message = Buffer.from('Some bytes');
6111cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
6121cb0ef41Sopenharmony_ciclient.send(message, 41234, 'localhost', (err) => {
6131cb0ef41Sopenharmony_ci  client.close();
6141cb0ef41Sopenharmony_ci});
6151cb0ef41Sopenharmony_ci```
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci```cjs
6181cb0ef41Sopenharmony_ciconst dgram = require('node:dgram');
6191cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
6201cb0ef41Sopenharmony_ci
6211cb0ef41Sopenharmony_ciconst message = Buffer.from('Some bytes');
6221cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
6231cb0ef41Sopenharmony_ciclient.send(message, 41234, 'localhost', (err) => {
6241cb0ef41Sopenharmony_ci  client.close();
6251cb0ef41Sopenharmony_ci});
6261cb0ef41Sopenharmony_ci```
6271cb0ef41Sopenharmony_ci
6281cb0ef41Sopenharmony_ciExample of sending a UDP packet composed of multiple buffers to a port on
6291cb0ef41Sopenharmony_ci`127.0.0.1`;
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci```mjs
6321cb0ef41Sopenharmony_ciimport dgram from 'node:dgram';
6331cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
6341cb0ef41Sopenharmony_ci
6351cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('Some ');
6361cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('bytes');
6371cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
6381cb0ef41Sopenharmony_ciclient.send([buf1, buf2], 41234, (err) => {
6391cb0ef41Sopenharmony_ci  client.close();
6401cb0ef41Sopenharmony_ci});
6411cb0ef41Sopenharmony_ci```
6421cb0ef41Sopenharmony_ci
6431cb0ef41Sopenharmony_ci```cjs
6441cb0ef41Sopenharmony_ciconst dgram = require('node:dgram');
6451cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
6461cb0ef41Sopenharmony_ci
6471cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('Some ');
6481cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('bytes');
6491cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
6501cb0ef41Sopenharmony_ciclient.send([buf1, buf2], 41234, (err) => {
6511cb0ef41Sopenharmony_ci  client.close();
6521cb0ef41Sopenharmony_ci});
6531cb0ef41Sopenharmony_ci```
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ciSending multiple buffers might be faster or slower depending on the
6561cb0ef41Sopenharmony_ciapplication and operating system. Run benchmarks to
6571cb0ef41Sopenharmony_cidetermine the optimal strategy on a case-by-case basis. Generally speaking,
6581cb0ef41Sopenharmony_cihowever, sending multiple buffers is faster.
6591cb0ef41Sopenharmony_ci
6601cb0ef41Sopenharmony_ciExample of sending a UDP packet using a socket connected to a port on
6611cb0ef41Sopenharmony_ci`localhost`:
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ci```mjs
6641cb0ef41Sopenharmony_ciimport dgram from 'node:dgram';
6651cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ciconst message = Buffer.from('Some bytes');
6681cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
6691cb0ef41Sopenharmony_ciclient.connect(41234, 'localhost', (err) => {
6701cb0ef41Sopenharmony_ci  client.send(message, (err) => {
6711cb0ef41Sopenharmony_ci    client.close();
6721cb0ef41Sopenharmony_ci  });
6731cb0ef41Sopenharmony_ci});
6741cb0ef41Sopenharmony_ci```
6751cb0ef41Sopenharmony_ci
6761cb0ef41Sopenharmony_ci```cjs
6771cb0ef41Sopenharmony_ciconst dgram = require('node:dgram');
6781cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ciconst message = Buffer.from('Some bytes');
6811cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
6821cb0ef41Sopenharmony_ciclient.connect(41234, 'localhost', (err) => {
6831cb0ef41Sopenharmony_ci  client.send(message, (err) => {
6841cb0ef41Sopenharmony_ci    client.close();
6851cb0ef41Sopenharmony_ci  });
6861cb0ef41Sopenharmony_ci});
6871cb0ef41Sopenharmony_ci```
6881cb0ef41Sopenharmony_ci
6891cb0ef41Sopenharmony_ci#### Note about UDP datagram size
6901cb0ef41Sopenharmony_ci
6911cb0ef41Sopenharmony_ciThe maximum size of an IPv4/v6 datagram depends on the `MTU`
6921cb0ef41Sopenharmony_ci(Maximum Transmission Unit) and on the `Payload Length` field size.
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ci* The `Payload Length` field is 16 bits wide, which means that a normal
6951cb0ef41Sopenharmony_ci  payload cannot exceed 64K octets including the internet header and data
6961cb0ef41Sopenharmony_ci  (65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header);
6971cb0ef41Sopenharmony_ci  this is generally true for loopback interfaces, but such long datagram
6981cb0ef41Sopenharmony_ci  messages are impractical for most hosts and networks.
6991cb0ef41Sopenharmony_ci
7001cb0ef41Sopenharmony_ci* The `MTU` is the largest size a given link layer technology can support for
7011cb0ef41Sopenharmony_ci  datagram messages. For any link, IPv4 mandates a minimum `MTU` of 68
7021cb0ef41Sopenharmony_ci  octets, while the recommended `MTU` for IPv4 is 576 (typically recommended
7031cb0ef41Sopenharmony_ci  as the `MTU` for dial-up type applications), whether they arrive whole or in
7041cb0ef41Sopenharmony_ci  fragments.
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci  For IPv6, the minimum `MTU` is 1280 octets. However, the mandatory minimum
7071cb0ef41Sopenharmony_ci  fragment reassembly buffer size is 1500 octets. The value of 68 octets is
7081cb0ef41Sopenharmony_ci  very small, since most current link layer technologies, like Ethernet, have a
7091cb0ef41Sopenharmony_ci  minimum `MTU` of 1500.
7101cb0ef41Sopenharmony_ci
7111cb0ef41Sopenharmony_ciIt is impossible to know in advance the MTU of each link through which
7121cb0ef41Sopenharmony_cia packet might travel. Sending a datagram greater than the receiver `MTU` will
7131cb0ef41Sopenharmony_cinot work because the packet will get silently dropped without informing the
7141cb0ef41Sopenharmony_cisource that the data did not reach its intended recipient.
7151cb0ef41Sopenharmony_ci
7161cb0ef41Sopenharmony_ci### `socket.setBroadcast(flag)`
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_ci<!-- YAML
7191cb0ef41Sopenharmony_ciadded: v0.6.9
7201cb0ef41Sopenharmony_ci-->
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci* `flag` {boolean}
7231cb0ef41Sopenharmony_ci
7241cb0ef41Sopenharmony_ciSets or clears the `SO_BROADCAST` socket option. When set to `true`, UDP
7251cb0ef41Sopenharmony_cipackets may be sent to a local interface's broadcast address.
7261cb0ef41Sopenharmony_ci
7271cb0ef41Sopenharmony_ciThis method throws `EBADF` if called on an unbound socket.
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci### `socket.setMulticastInterface(multicastInterface)`
7301cb0ef41Sopenharmony_ci
7311cb0ef41Sopenharmony_ci<!-- YAML
7321cb0ef41Sopenharmony_ciadded: v8.6.0
7331cb0ef41Sopenharmony_ci-->
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ci* `multicastInterface` {string}
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci_All references to scope in this section are referring to
7381cb0ef41Sopenharmony_ci[IPv6 Zone Indices][], which are defined by [RFC 4007][]. In string form, an IP
7391cb0ef41Sopenharmony_ciwith a scope index is written as `'IP%scope'` where scope is an interface name
7401cb0ef41Sopenharmony_cior interface number._
7411cb0ef41Sopenharmony_ci
7421cb0ef41Sopenharmony_ciSets the default outgoing multicast interface of the socket to a chosen
7431cb0ef41Sopenharmony_ciinterface or back to system interface selection. The `multicastInterface` must
7441cb0ef41Sopenharmony_cibe a valid string representation of an IP from the socket's family.
7451cb0ef41Sopenharmony_ci
7461cb0ef41Sopenharmony_ciFor IPv4 sockets, this should be the IP configured for the desired physical
7471cb0ef41Sopenharmony_ciinterface. All packets sent to multicast on the socket will be sent on the
7481cb0ef41Sopenharmony_ciinterface determined by the most recent successful use of this call.
7491cb0ef41Sopenharmony_ci
7501cb0ef41Sopenharmony_ciFor IPv6 sockets, `multicastInterface` should include a scope to indicate the
7511cb0ef41Sopenharmony_ciinterface as in the examples that follow. In IPv6, individual `send` calls can
7521cb0ef41Sopenharmony_cialso use explicit scope in addresses, so only packets sent to a multicast
7531cb0ef41Sopenharmony_ciaddress without specifying an explicit scope are affected by the most recent
7541cb0ef41Sopenharmony_cisuccessful use of this call.
7551cb0ef41Sopenharmony_ci
7561cb0ef41Sopenharmony_ciThis method throws `EBADF` if called on an unbound socket.
7571cb0ef41Sopenharmony_ci
7581cb0ef41Sopenharmony_ci#### Example: IPv6 outgoing multicast interface
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ciOn most systems, where scope format uses the interface name:
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ci```js
7631cb0ef41Sopenharmony_ciconst socket = dgram.createSocket('udp6');
7641cb0ef41Sopenharmony_ci
7651cb0ef41Sopenharmony_cisocket.bind(1234, () => {
7661cb0ef41Sopenharmony_ci  socket.setMulticastInterface('::%eth1');
7671cb0ef41Sopenharmony_ci});
7681cb0ef41Sopenharmony_ci```
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ciOn Windows, where scope format uses an interface number:
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ci```js
7731cb0ef41Sopenharmony_ciconst socket = dgram.createSocket('udp6');
7741cb0ef41Sopenharmony_ci
7751cb0ef41Sopenharmony_cisocket.bind(1234, () => {
7761cb0ef41Sopenharmony_ci  socket.setMulticastInterface('::%2');
7771cb0ef41Sopenharmony_ci});
7781cb0ef41Sopenharmony_ci```
7791cb0ef41Sopenharmony_ci
7801cb0ef41Sopenharmony_ci#### Example: IPv4 outgoing multicast interface
7811cb0ef41Sopenharmony_ci
7821cb0ef41Sopenharmony_ciAll systems use an IP of the host on the desired physical interface:
7831cb0ef41Sopenharmony_ci
7841cb0ef41Sopenharmony_ci```js
7851cb0ef41Sopenharmony_ciconst socket = dgram.createSocket('udp4');
7861cb0ef41Sopenharmony_ci
7871cb0ef41Sopenharmony_cisocket.bind(1234, () => {
7881cb0ef41Sopenharmony_ci  socket.setMulticastInterface('10.0.0.2');
7891cb0ef41Sopenharmony_ci});
7901cb0ef41Sopenharmony_ci```
7911cb0ef41Sopenharmony_ci
7921cb0ef41Sopenharmony_ci#### Call results
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ciA call on a socket that is not ready to send or no longer open may throw a _Not
7951cb0ef41Sopenharmony_cirunning_ [`Error`][].
7961cb0ef41Sopenharmony_ci
7971cb0ef41Sopenharmony_ciIf `multicastInterface` can not be parsed into an IP then an _EINVAL_
7981cb0ef41Sopenharmony_ci[`System Error`][] is thrown.
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ciOn IPv4, if `multicastInterface` is a valid address but does not match any
8011cb0ef41Sopenharmony_ciinterface, or if the address does not match the family then
8021cb0ef41Sopenharmony_cia [`System Error`][] such as `EADDRNOTAVAIL` or `EPROTONOSUP` is thrown.
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_ciOn IPv6, most errors with specifying or omitting scope will result in the socket
8051cb0ef41Sopenharmony_cicontinuing to use (or returning to) the system's default interface selection.
8061cb0ef41Sopenharmony_ci
8071cb0ef41Sopenharmony_ciA socket's address family's ANY address (IPv4 `'0.0.0.0'` or IPv6 `'::'`) can be
8081cb0ef41Sopenharmony_ciused to return control of the sockets default outgoing interface to the system
8091cb0ef41Sopenharmony_cifor future multicast packets.
8101cb0ef41Sopenharmony_ci
8111cb0ef41Sopenharmony_ci### `socket.setMulticastLoopback(flag)`
8121cb0ef41Sopenharmony_ci
8131cb0ef41Sopenharmony_ci<!-- YAML
8141cb0ef41Sopenharmony_ciadded: v0.3.8
8151cb0ef41Sopenharmony_ci-->
8161cb0ef41Sopenharmony_ci
8171cb0ef41Sopenharmony_ci* `flag` {boolean}
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ciSets or clears the `IP_MULTICAST_LOOP` socket option. When set to `true`,
8201cb0ef41Sopenharmony_cimulticast packets will also be received on the local interface.
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ciThis method throws `EBADF` if called on an unbound socket.
8231cb0ef41Sopenharmony_ci
8241cb0ef41Sopenharmony_ci### `socket.setMulticastTTL(ttl)`
8251cb0ef41Sopenharmony_ci
8261cb0ef41Sopenharmony_ci<!-- YAML
8271cb0ef41Sopenharmony_ciadded: v0.3.8
8281cb0ef41Sopenharmony_ci-->
8291cb0ef41Sopenharmony_ci
8301cb0ef41Sopenharmony_ci* `ttl` {integer}
8311cb0ef41Sopenharmony_ci
8321cb0ef41Sopenharmony_ciSets the `IP_MULTICAST_TTL` socket option. While TTL generally stands for
8331cb0ef41Sopenharmony_ci"Time to Live", in this context it specifies the number of IP hops that a
8341cb0ef41Sopenharmony_cipacket is allowed to travel through, specifically for multicast traffic. Each
8351cb0ef41Sopenharmony_cirouter or gateway that forwards a packet decrements the TTL. If the TTL is
8361cb0ef41Sopenharmony_cidecremented to 0 by a router, it will not be forwarded.
8371cb0ef41Sopenharmony_ci
8381cb0ef41Sopenharmony_ciThe `ttl` argument may be between 0 and 255. The default on most systems is `1`.
8391cb0ef41Sopenharmony_ci
8401cb0ef41Sopenharmony_ciThis method throws `EBADF` if called on an unbound socket.
8411cb0ef41Sopenharmony_ci
8421cb0ef41Sopenharmony_ci### `socket.setRecvBufferSize(size)`
8431cb0ef41Sopenharmony_ci
8441cb0ef41Sopenharmony_ci<!-- YAML
8451cb0ef41Sopenharmony_ciadded: v8.7.0
8461cb0ef41Sopenharmony_ci-->
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ci* `size` {integer}
8491cb0ef41Sopenharmony_ci
8501cb0ef41Sopenharmony_ciSets the `SO_RCVBUF` socket option. Sets the maximum socket receive buffer
8511cb0ef41Sopenharmony_ciin bytes.
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ciThis method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ci### `socket.setSendBufferSize(size)`
8561cb0ef41Sopenharmony_ci
8571cb0ef41Sopenharmony_ci<!-- YAML
8581cb0ef41Sopenharmony_ciadded: v8.7.0
8591cb0ef41Sopenharmony_ci-->
8601cb0ef41Sopenharmony_ci
8611cb0ef41Sopenharmony_ci* `size` {integer}
8621cb0ef41Sopenharmony_ci
8631cb0ef41Sopenharmony_ciSets the `SO_SNDBUF` socket option. Sets the maximum socket send buffer
8641cb0ef41Sopenharmony_ciin bytes.
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ciThis method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
8671cb0ef41Sopenharmony_ci
8681cb0ef41Sopenharmony_ci### `socket.setTTL(ttl)`
8691cb0ef41Sopenharmony_ci
8701cb0ef41Sopenharmony_ci<!-- YAML
8711cb0ef41Sopenharmony_ciadded: v0.1.101
8721cb0ef41Sopenharmony_ci-->
8731cb0ef41Sopenharmony_ci
8741cb0ef41Sopenharmony_ci* `ttl` {integer}
8751cb0ef41Sopenharmony_ci
8761cb0ef41Sopenharmony_ciSets the `IP_TTL` socket option. While TTL generally stands for "Time to Live",
8771cb0ef41Sopenharmony_ciin this context it specifies the number of IP hops that a packet is allowed to
8781cb0ef41Sopenharmony_citravel through. Each router or gateway that forwards a packet decrements the
8791cb0ef41Sopenharmony_ciTTL. If the TTL is decremented to 0 by a router, it will not be forwarded.
8801cb0ef41Sopenharmony_ciChanging TTL values is typically done for network probes or when multicasting.
8811cb0ef41Sopenharmony_ci
8821cb0ef41Sopenharmony_ciThe `ttl` argument may be between 1 and 255. The default on most systems
8831cb0ef41Sopenharmony_ciis 64.
8841cb0ef41Sopenharmony_ci
8851cb0ef41Sopenharmony_ciThis method throws `EBADF` if called on an unbound socket.
8861cb0ef41Sopenharmony_ci
8871cb0ef41Sopenharmony_ci### `socket.unref()`
8881cb0ef41Sopenharmony_ci
8891cb0ef41Sopenharmony_ci<!-- YAML
8901cb0ef41Sopenharmony_ciadded: v0.9.1
8911cb0ef41Sopenharmony_ci-->
8921cb0ef41Sopenharmony_ci
8931cb0ef41Sopenharmony_ci* Returns: {dgram.Socket}
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_ciBy default, binding a socket will cause it to block the Node.js process from
8961cb0ef41Sopenharmony_ciexiting as long as the socket is open. The `socket.unref()` method can be used
8971cb0ef41Sopenharmony_cito exclude the socket from the reference counting that keeps the Node.js
8981cb0ef41Sopenharmony_ciprocess active, allowing the process to exit even if the socket is still
8991cb0ef41Sopenharmony_cilistening.
9001cb0ef41Sopenharmony_ci
9011cb0ef41Sopenharmony_ciCalling `socket.unref()` multiple times will have no addition effect.
9021cb0ef41Sopenharmony_ci
9031cb0ef41Sopenharmony_ciThe `socket.unref()` method returns a reference to the socket so calls can be
9041cb0ef41Sopenharmony_cichained.
9051cb0ef41Sopenharmony_ci
9061cb0ef41Sopenharmony_ci## `node:dgram` module functions
9071cb0ef41Sopenharmony_ci
9081cb0ef41Sopenharmony_ci### `dgram.createSocket(options[, callback])`
9091cb0ef41Sopenharmony_ci
9101cb0ef41Sopenharmony_ci<!-- YAML
9111cb0ef41Sopenharmony_ciadded: v0.11.13
9121cb0ef41Sopenharmony_cichanges:
9131cb0ef41Sopenharmony_ci  - version: v15.8.0
9141cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/37026
9151cb0ef41Sopenharmony_ci    description: AbortSignal support was added.
9161cb0ef41Sopenharmony_ci  - version: v11.4.0
9171cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/23798
9181cb0ef41Sopenharmony_ci    description: The `ipv6Only` option is supported.
9191cb0ef41Sopenharmony_ci  - version: v8.7.0
9201cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/13623
9211cb0ef41Sopenharmony_ci    description: The `recvBufferSize` and `sendBufferSize` options are
9221cb0ef41Sopenharmony_ci                 supported now.
9231cb0ef41Sopenharmony_ci  - version: v8.6.0
9241cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/14560
9251cb0ef41Sopenharmony_ci    description: The `lookup` option is supported.
9261cb0ef41Sopenharmony_ci-->
9271cb0ef41Sopenharmony_ci
9281cb0ef41Sopenharmony_ci* `options` {Object} Available options are:
9291cb0ef41Sopenharmony_ci  * `type` {string} The family of socket. Must be either `'udp4'` or `'udp6'`.
9301cb0ef41Sopenharmony_ci    Required.
9311cb0ef41Sopenharmony_ci  * `reuseAddr` {boolean} When `true` [`socket.bind()`][] will reuse the
9321cb0ef41Sopenharmony_ci    address, even if another process has already bound a socket on it.
9331cb0ef41Sopenharmony_ci    **Default:** `false`.
9341cb0ef41Sopenharmony_ci  * `ipv6Only` {boolean} Setting `ipv6Only` to `true` will
9351cb0ef41Sopenharmony_ci    disable dual-stack support, i.e., binding to address `::` won't make
9361cb0ef41Sopenharmony_ci    `0.0.0.0` be bound. **Default:** `false`.
9371cb0ef41Sopenharmony_ci  * `recvBufferSize` {number} Sets the `SO_RCVBUF` socket value.
9381cb0ef41Sopenharmony_ci  * `sendBufferSize` {number} Sets the `SO_SNDBUF` socket value.
9391cb0ef41Sopenharmony_ci  * `lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][].
9401cb0ef41Sopenharmony_ci  * `signal` {AbortSignal} An AbortSignal that may be used to close a socket.
9411cb0ef41Sopenharmony_ci* `callback` {Function} Attached as a listener for `'message'` events. Optional.
9421cb0ef41Sopenharmony_ci* Returns: {dgram.Socket}
9431cb0ef41Sopenharmony_ci
9441cb0ef41Sopenharmony_ciCreates a `dgram.Socket` object. Once the socket is created, calling
9451cb0ef41Sopenharmony_ci[`socket.bind()`][] will instruct the socket to begin listening for datagram
9461cb0ef41Sopenharmony_cimessages. When `address` and `port` are not passed to [`socket.bind()`][] the
9471cb0ef41Sopenharmony_cimethod will bind the socket to the "all interfaces" address on a random port
9481cb0ef41Sopenharmony_ci(it does the right thing for both `udp4` and `udp6` sockets). The bound address
9491cb0ef41Sopenharmony_ciand port can be retrieved using [`socket.address().address`][] and
9501cb0ef41Sopenharmony_ci[`socket.address().port`][].
9511cb0ef41Sopenharmony_ci
9521cb0ef41Sopenharmony_ciIf the `signal` option is enabled, calling `.abort()` on the corresponding
9531cb0ef41Sopenharmony_ci`AbortController` is similar to calling `.close()` on the socket:
9541cb0ef41Sopenharmony_ci
9551cb0ef41Sopenharmony_ci```js
9561cb0ef41Sopenharmony_ciconst controller = new AbortController();
9571cb0ef41Sopenharmony_ciconst { signal } = controller;
9581cb0ef41Sopenharmony_ciconst server = dgram.createSocket({ type: 'udp4', signal });
9591cb0ef41Sopenharmony_ciserver.on('message', (msg, rinfo) => {
9601cb0ef41Sopenharmony_ci  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
9611cb0ef41Sopenharmony_ci});
9621cb0ef41Sopenharmony_ci// Later, when you want to close the server.
9631cb0ef41Sopenharmony_cicontroller.abort();
9641cb0ef41Sopenharmony_ci```
9651cb0ef41Sopenharmony_ci
9661cb0ef41Sopenharmony_ci### `dgram.createSocket(type[, callback])`
9671cb0ef41Sopenharmony_ci
9681cb0ef41Sopenharmony_ci<!-- YAML
9691cb0ef41Sopenharmony_ciadded: v0.1.99
9701cb0ef41Sopenharmony_ci-->
9711cb0ef41Sopenharmony_ci
9721cb0ef41Sopenharmony_ci* `type` {string} Either `'udp4'` or `'udp6'`.
9731cb0ef41Sopenharmony_ci* `callback` {Function} Attached as a listener to `'message'` events.
9741cb0ef41Sopenharmony_ci* Returns: {dgram.Socket}
9751cb0ef41Sopenharmony_ci
9761cb0ef41Sopenharmony_ciCreates a `dgram.Socket` object of the specified `type`.
9771cb0ef41Sopenharmony_ci
9781cb0ef41Sopenharmony_ciOnce the socket is created, calling [`socket.bind()`][] will instruct the
9791cb0ef41Sopenharmony_cisocket to begin listening for datagram messages. When `address` and `port` are
9801cb0ef41Sopenharmony_cinot passed to [`socket.bind()`][] the method will bind the socket to the "all
9811cb0ef41Sopenharmony_ciinterfaces" address on a random port (it does the right thing for both `udp4`
9821cb0ef41Sopenharmony_ciand `udp6` sockets). The bound address and port can be retrieved using
9831cb0ef41Sopenharmony_ci[`socket.address().address`][] and [`socket.address().port`][].
9841cb0ef41Sopenharmony_ci
9851cb0ef41Sopenharmony_ci[IPv6 Zone Indices]: https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses
9861cb0ef41Sopenharmony_ci[RFC 4007]: https://tools.ietf.org/html/rfc4007
9871cb0ef41Sopenharmony_ci[`'close'`]: #event-close
9881cb0ef41Sopenharmony_ci[`ERR_SOCKET_BAD_PORT`]: errors.md#err_socket_bad_port
9891cb0ef41Sopenharmony_ci[`ERR_SOCKET_BUFFER_SIZE`]: errors.md#err_socket_buffer_size
9901cb0ef41Sopenharmony_ci[`ERR_SOCKET_DGRAM_IS_CONNECTED`]: errors.md#err_socket_dgram_is_connected
9911cb0ef41Sopenharmony_ci[`ERR_SOCKET_DGRAM_NOT_CONNECTED`]: errors.md#err_socket_dgram_not_connected
9921cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error
9931cb0ef41Sopenharmony_ci[`System Error`]: errors.md#class-systemerror
9941cb0ef41Sopenharmony_ci[`close()`]: #socketclosecallback
9951cb0ef41Sopenharmony_ci[`cluster`]: cluster.md
9961cb0ef41Sopenharmony_ci[`connect()`]: #socketconnectport-address-callback
9971cb0ef41Sopenharmony_ci[`dgram.createSocket()`]: #dgramcreatesocketoptions-callback
9981cb0ef41Sopenharmony_ci[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
9991cb0ef41Sopenharmony_ci[`socket.address().address`]: #socketaddress
10001cb0ef41Sopenharmony_ci[`socket.address().port`]: #socketaddress
10011cb0ef41Sopenharmony_ci[`socket.bind()`]: #socketbindport-address-callback
10021cb0ef41Sopenharmony_ci[`socket.close()`]: #socketclosecallback
10031cb0ef41Sopenharmony_ci[byte length]: buffer.md#static-method-bufferbytelengthstring-encoding
1004