Created
April 5, 2014 12:27
-
-
Save arselzer/9991347 to your computer and use it in GitHub Desktop.
Node.js UDP
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 udp = require("dgram"); | |
var socket = udp.createSocket("udp4"); | |
var msg = new Buffer("ping"); | |
socket.send(msg, 0, msg.length, 8232, "localhost"); | |
socket.on("message", function(msg, info) { | |
var res = new Buffer("client -> " + info.address + ":" + info.port); | |
console.log(msg.toString()); | |
setTimeout(function() { | |
socket.send(res, 0, res.length, info.port, info.address); | |
}, 600); | |
}); |
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
#!/bin/bash | |
node server.js & | |
sleep 1 | |
node client.js |
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 udp = require("dgram"); | |
var socket = udp.createSocket("udp4"); | |
socket.bind(8232); | |
console.log("listening on port 8232"); | |
socket.on("message", function(msg, info) { | |
var res = new Buffer("server -> " + info.address + ":" + info.port); | |
console.log(msg.toString()); | |
socket.send(res, 0, res.length, info.port, info.address); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment