dgram socket is already connected
Production Risk
Low — call disconnect() before reconnecting a UDP socket.
Thrown when dgram.connect() is called on a UDP socket that is already in a connected state. A connected UDP socket has a fixed default peer; calling connect() again without first calling disconnect() is not permitted.
- 1Calling socket.connect() twice on the same dgram socket
- 2Calling addMembership() on a connected socket (some operations require unconnected sockets)
Triggered when connect() is called on a dgram socket that has already been connected.
const dgram = require('dgram');
const s = dgram.createSocket('udp4');
s.connect(41234, 'localhost', () => {
s.connect(41235, 'localhost'); // throws — already connected
});expected output
Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connected
Fix
Call disconnect() before connecting to a new peer
WHEN When switching the connected peer
s.disconnect();
s.connect(41235, 'localhost', () => {
// now connected to new peer
});Why this works
disconnect() releases the current peer association, allowing a new connect().
const dgram = require('dgram');
const s = dgram.createSocket('udp4');
s.connect(41234, 'localhost', () => {
s.connect(41235, 'localhost'); // throws — already connected
}); // this triggers ERR_SOCKET_DGRAM_IS_CONNECTEDtry {
// operation that may throw ERR_SOCKET_DGRAM_IS_CONNECTED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_SOCKET_DGRAM_IS_CONNECTED') {
console.error('ERR_SOCKET_DGRAM_IS_CONNECTED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_socket_dgram_is_connected(...args) {
// validate args here
return performOperation(...args)
}✕ Call connect() twice without disconnecting
A connected dgram socket has a fixed peer; the second connect throws.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev