Skip to content

Instantly share code, notes, and snippets.

@geekscape
Created September 29, 2015 00:57
Show Gist options
  • Save geekscape/a8b9b5aebd8add74779f to your computer and use it in GitHub Desktop.
Save geekscape/a8b9b5aebd8add74779f to your computer and use it in GitHub Desktop.
var dgram = require('dgram'),
fs = require('fs'),
readline = require('readline');
var HOST = 'localhost';
var PORT = 4149;
//var client = dgram.createSocket('udp6');
var client = dgram.createSocket('udp4');
client.bind(0);
client.on('message', function (msg, rinfo) {
console.log('Received: ' + rinfo.address + ':' + rinfo.port + ': ' + msg);
});
function transmit(message) {
var buffer = new Buffer(message);
client.send(buffer, 0, buffer.length, PORT, HOST,
function(error, bytes) {
if (error) throw error;
console.log('Sent to: ' + HOST + ':' + PORT);
}
);
}
var filename = process.argv[2];
if (! filename) filename = '/dev/stdin';
var input = readline.createInterface({
input: fs.createReadStream(filename),
output: process.stdout,
terminal: false
});
input.on('line', function(line) {
transmit(line + ' ');
});
input.on('close', function() {
client.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment