-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.js
More file actions
32 lines (24 loc) · 1.13 KB
/
example.js
File metadata and controls
32 lines (24 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict'
const {createSocket} = require('dgram')
const {getsockopt, setsockopt} = require('.')
// https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/sys/socket.h#L525
const SOL_SOCKET = 0xffff
// https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/sys/socket.h#L130
const SO_REUSEADDR = 0x0004
// https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/sys/socket.h#L165
const SO_SNDBUF = 0x1001
const socket = createSocket({type: 'udp4'})
socket.on('error', (err) => {
console.error(err)
process.exit(1)
})
socket.bind(1234, '0.0.0.0', () => {
console.log('SO_REUSEADDR is', !!getsockopt(socket, SOL_SOCKET, SO_REUSEADDR))
setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, +true) // pass `1` for true
console.log('SO_REUSEADDR is now', !!getsockopt(socket, SOL_SOCKET, SO_REUSEADDR))
console.log('SO_SNDBUF is', getsockopt(socket, SOL_SOCKET, SO_SNDBUF))
setsockopt(socket, SOL_SOCKET, SO_SNDBUF, 1024)
console.log('SO_SNDBUF is now', getsockopt(socket, SOL_SOCKET, SO_SNDBUF))
// do something with the socket here instead!
socket.close()
})