Created
October 10, 2018 20:37
-
-
Save getnamo/8117fdc64209af086ce0337310c52a51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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