Skip to content

Instantly share code, notes, and snippets.

@yoshuawuyts
Last active April 2, 2019 15:22
Show Gist options
  • Save yoshuawuyts/e184f8c2702a051526df13628f2fd21d to your computer and use it in GitHub Desktop.
Save yoshuawuyts/e184f8c2702a051526df13628f2fd21d to your computer and use it in GitHub Desktop.
#!/usr/local/bin/node
let dgram = require('dgram')
let socket = dgram.createSocket('udp4', (msg, rinfo) => {
let peer = `${rinfo.address}:${rinfo.port}`
socket.send(msg, peer, (err) => {
if (err) peer.close()
console.log(`Sent ${rinfo.size} of ${rinfo.size} bytes to ${peer}`)
})
})
socket.bind(8080, () => {
let address = socket.address()
console.log(`Listening on ${address.address}:${address.port}`)
})
@yoshuawuyts
Copy link
Author

yoshuawuyts commented Apr 1, 2019

We're trying to show a realistic depiction of how people would likely write a UDP server in Node. This is how I'd go about it if I was tasked with building a UDP server for a contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment