Skip to content

Instantly share code, notes, and snippets.

@getnamo
Created October 10, 2018 20:37
Show Gist options
  • Save getnamo/8117fdc64209af086ce0337310c52a51 to your computer and use it in GitHub Desktop.
Save getnamo/8117fdc64209af086ce0337310c52a51 to your computer and use it in GitHub Desktop.
const RECEIVE_PORT = 3001;
const SEND_PORT = 3002;
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('listening', function () {
let address = server.address();
console.log(`UDP server listening on ${address.address}:${address.port}`);
});
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
//echo the message on sending port and append 'echo: ' string before message
const echoBuf = Buffer.from('echo: ');
server.send([echoBuf, message], SEND_PORT, remote.address);
});
server.bind(RECEIVE_PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment