Skip to content

Commit 51cdd10

Browse files
mkrawczukaddaleax
authored andcommitted
doc: mention errors thrown by methods called on an unbound dgram.Socket
PR-URL: #33983 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6d22ae3 commit 51cdd10

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

doc/api/dgram.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Tells the kernel to join a multicast group at the given `multicastAddress` and
108108
one interface and will add membership to it. To add membership to every
109109
available interface, call `addMembership` multiple times, once per interface.
110110

111+
When called on an unbound socket, this method will implicitly bind to a random
112+
port, listening on all interfaces.
113+
111114
When sharing a UDP socket across multiple `cluster` workers, the
112115
`socket.addMembership()` function must be called only once or an
113116
`EADDRINUSE` error will occur:
@@ -141,6 +144,9 @@ is not specified, the operating system will choose one interface and will add
141144
membership to it. To add membership to every available interface, call
142145
`socket.addSourceSpecificMembership()` multiple times, once per interface.
143146

147+
When called on an unbound socket, this method will implicitly bind to a random
148+
port, listening on all interfaces.
149+
144150
### `socket.address()`
145151
<!-- YAML
146152
added: v0.1.99
@@ -152,6 +158,8 @@ Returns an object containing the address information for a socket.
152158
For UDP sockets, this object will contain `address`, `family` and `port`
153159
properties.
154160

161+
This method throws `EBADF` if called on an unbound socket.
162+
155163
### `socket.bind([port][, address][, callback])`
156164
<!-- YAML
157165
added: v0.1.99
@@ -296,8 +304,9 @@ added: v12.0.0
296304
-->
297305

298306
A synchronous function that disassociates a connected `dgram.Socket` from
299-
its remote address. Trying to call `disconnect()` on an already disconnected
300-
socket will result in an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`][] exception.
307+
its remote address. Trying to call `disconnect()` on an unbound or already
308+
disconnected socket will result in an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`][]
309+
exception.
301310

302311
### `socket.dropMembership(multicastAddress[, multicastInterface])`
303312
<!-- YAML
@@ -340,13 +349,17 @@ added: v8.7.0
340349

341350
* Returns: {number} the `SO_RCVBUF` socket receive buffer size in bytes.
342351

352+
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
353+
343354
### `socket.getSendBufferSize()`
344355
<!-- YAML
345356
added: v8.7.0
346357
-->
347358

348359
* Returns: {number} the `SO_SNDBUF` socket send buffer size in bytes.
349360

361+
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
362+
350363
### `socket.ref()`
351364
<!-- YAML
352365
added: v0.9.1
@@ -373,8 +386,8 @@ added: v12.0.0
373386
* Returns: {Object}
374387

375388
Returns an object containing the `address`, `family`, and `port` of the remote
376-
endpoint. It throws an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`][] exception if the
377-
socket is not connected.
389+
endpoint. This method throws an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`][] exception
390+
if the socket is not connected.
378391

379392
### `socket.send(msg[, offset, length][, port][, address][, callback])`
380393
<!-- YAML
@@ -444,6 +457,8 @@ the error is emitted as an `'error'` event on the `socket` object.
444457
Offset and length are optional but both *must* be set if either are used.
445458
They are supported only when the first argument is a `Buffer` or `Uint8Array`.
446459

460+
This method throws [`ERR_SOCKET_BAD_PORT`][] if called on an unbound socket.
461+
447462
Example of sending a UDP packet to a port on `localhost`;
448463

449464
```js
@@ -524,6 +539,8 @@ added: v0.6.9
524539
Sets or clears the `SO_BROADCAST` socket option. When set to `true`, UDP
525540
packets may be sent to a local interface's broadcast address.
526541

542+
This method throws `EBADF` if called on an unbound socket.
543+
527544
### `socket.setMulticastInterface(multicastInterface)`
528545
<!-- YAML
529546
added: v8.6.0
@@ -550,6 +567,8 @@ also use explicit scope in addresses, so only packets sent to a multicast
550567
address without specifying an explicit scope are affected by the most recent
551568
successful use of this call.
552569

570+
This method throws `EBADF` if called on an unbound socket.
571+
553572
#### Example: IPv6 outgoing multicast interface
554573

555574
On most systems, where scope format uses the interface name:
@@ -612,6 +631,8 @@ added: v0.3.8
612631
Sets or clears the `IP_MULTICAST_LOOP` socket option. When set to `true`,
613632
multicast packets will also be received on the local interface.
614633

634+
This method throws `EBADF` if called on an unbound socket.
635+
615636
### `socket.setMulticastTTL(ttl)`
616637
<!-- YAML
617638
added: v0.3.8
@@ -627,6 +648,8 @@ decremented to 0 by a router, it will not be forwarded.
627648

628649
The `ttl` argument may be between 0 and 255. The default on most systems is `1`.
629650

651+
This method throws `EBADF` if called on an unbound socket.
652+
630653
### `socket.setRecvBufferSize(size)`
631654
<!-- YAML
632655
added: v8.7.0
@@ -637,6 +660,8 @@ added: v8.7.0
637660
Sets the `SO_RCVBUF` socket option. Sets the maximum socket receive buffer
638661
in bytes.
639662

663+
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
664+
640665
### `socket.setSendBufferSize(size)`
641666
<!-- YAML
642667
added: v8.7.0
@@ -647,6 +672,8 @@ added: v8.7.0
647672
Sets the `SO_SNDBUF` socket option. Sets the maximum socket send buffer
648673
in bytes.
649674

675+
This method throws [`ERR_SOCKET_BUFFER_SIZE`][] if called on an unbound socket.
676+
650677
### `socket.setTTL(ttl)`
651678
<!-- YAML
652679
added: v0.1.101
@@ -663,6 +690,8 @@ Changing TTL values is typically done for network probes or when multicasting.
663690
The `ttl` argument may be between between 1 and 255. The default on most systems
664691
is 64.
665692

693+
This method throws `EBADF` if called on an unbound socket.
694+
666695
### `socket.unref()`
667696
<!-- YAML
668697
added: v0.9.1
@@ -741,6 +770,8 @@ and `udp6` sockets). The bound address and port can be retrieved using
741770
[`socket.address().address`][] and [`socket.address().port`][].
742771

743772
[`'close'`]: #dgram_event_close
773+
[`ERR_SOCKET_BAD_PORT`]: errors.html#errors_err_socket_bad_port
774+
[`ERR_SOCKET_BUFFER_SIZE`]: errors.html#errors_err_socket_buffer_size
744775
[`ERR_SOCKET_DGRAM_IS_CONNECTED`]: errors.html#errors_err_socket_dgram_is_connected
745776
[`ERR_SOCKET_DGRAM_NOT_CONNECTED`]: errors.html#errors_err_socket_dgram_not_connected
746777
[`Error`]: errors.html#errors_class_error

0 commit comments

Comments
 (0)