Created
September 29, 2015 00:57
-
-
Save geekscape/a8b9b5aebd8add74779f 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
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