Usage in Deno
```typescript import * as mod from "node:node__dgram.d.ts"; ```The `node:dgram` module provides an implementation of UDP datagram sockets.
```js
import dgram from 'node:dgram';
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
console.error(`server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
server.bind(41234);
// Prints: server listening 0.0.0.0:41234
```
c
Socket
> [!WARNING] Deno compatibility
> The following methods are non-functional stubs:
> - addMembership
> - addSourceSpecificMembership
> - dropMembership
> - dropSourceSpecificMembership
> - setBroadcast
> - setMulticastInterface
> - setMulticastLoopback
> - setMulticastTtl
> - setTtl
>
Encapsulates the datagram functionality.
New instances of `dgram.Socket` are created using [createSocket](.././node__dgram.d.ts/~/createSocket).
The `new` keyword is not to be used to create `dgram.Socket` instances.
- addListener
- addMembership
- addSourceSpecificMembership
- address
- bind
- close
- connect
- disconnect
- dropMembership
- dropSourceSpecificMembership
- emit
- getRecvBufferSize
- getSendBufferSize
- getSendQueueCount
- getSendQueueSize
- on
- once
- prependListener
- prependOnceListener
- ref
- remoteAddress
- send
- setBroadcast
- setMulticastInterface
- setMulticastLoopback
- setMulticastTTL
- setRecvBufferSize
- setSendBufferSize
- setTTL
- unref
f
createSocket
Creates 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:
```js
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();
```
I
I
I
T
SocketType
No documentation available