Skip to content

Instantly share code, notes, and snippets.

@Kalki5
Created December 12, 2019 11:07
Show Gist options
  • Save Kalki5/64fe38cc8a45b02a14aaaaa3fa955b06 to your computer and use it in GitHub Desktop.
Save Kalki5/64fe38cc8a45b02a14aaaaa3fa955b06 to your computer and use it in GitHub Desktop.
Simple Node UDP Server and Client
var PORT = 33333;
var HOST = '0.0.0.0';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
server.on('listening', function() {
var 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);
});
server.bind(PORT, HOST);
var PORT = 33333;
var HOST = '127.0.0.1';
var dgram = require('dgram');
var message = new Buffer('My KungFu is Good!');
var client = dgram.createSocket('udp4');
var efun = function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
client.close();
};
client.send(message, 0, message.length, PORT, HOST, efun);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment