Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created October 8, 2010 18:56
Show Gist options
  • Save rjungemann/617324 to your computer and use it in GitHub Desktop.
Save rjungemann/617324 to your computer and use it in GitHub Desktop.
UDP client and server in Node
var client = dgram.createSocket("udp4");
client.on("message", function(data, info) {
console.log("got: " + data.toString("utf8") + " from " + info.address);
});
client.on("listening", function() {
console.log("client listening " + client.address().address);
setTimeout(function() {
var message = new Buffer("A message at " + (new Date()));
client.send(message, 0, message.length, 8001, "localhost");
}, 5000);
});
client.bind(8002, "localhost");
var server = dgram.createSocket("udp4");
server.on('message', function(data, info) {
console.log(data.toString("utf8"), info);
});
server.bind(8001, "localhost");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment