Skip to content

Instantly share code, notes, and snippets.

@grizmio
Forked from PierBover/client.js
Created July 22, 2022 23:02
Show Gist options
  • Save grizmio/5b02154f69d105df8333d3af9e993108 to your computer and use it in GitHub Desktop.
Save grizmio/5b02154f69d105df8333d3af9e993108 to your computer and use it in GitHub Desktop.
Node.js UDP Broadcast example
const dgram = require('dgram');
const message = new Buffer('Server?');
const socket = dgram.createSocket('udp4');
socket.on('listening', function () {
socket.setBroadcast(true);
setInterval(() => {
socket.send(message, 0, message.length, 5555, '255.255.255.255');
}, 5000);
});
socket.on('message', function (message, remote) {
console.log('CLIENT RECEIVED: ', remote.address + ':' + remote.port +' - ' + message);
});
socket.bind('8888');
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
socket.on('listening', function () {
const address = socket.address();
console.log('UDP socket listening on ' + address.address + ":" + address.port);
});
socket.on('message', function (message, remote) {
console.log('SERVER RECEIVED:', remote.address + ':' + remote.port +' - ' + message);
const response = "Hellow there!";
socket.send(response, 0, response.length, remote.port, remote.address);
});
socket.bind('5555');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment