-
-
Save logic855/0a41905baa800af2fd35 to your computer and use it in GitHub Desktop.
UDP client and server in Node.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
log = require('sys').log | |
dgram = require('./lib/dgram') | |
var Buffer = require('buffer').Buffer; | |
var endat = 10; | |
var count = 0; | |
socket = dgram.createSocket(); | |
socket.addListener('message', function (msg, rinfo) { | |
log('got message from '+ rinfo.address +' port: '+ rinfo.port); | |
log('data len: '+ rinfo.size + " data: "+ msg.toString('ascii', 0, rinfo.size)); | |
socket.send(rinfo.port, rinfo.address, msg, 0, rinfo.size); | |
count += 1 | |
if (count == endat) { | |
socket.close(); | |
process.exit(); | |
} | |
}); | |
socket.bind(8000); | |
setInterval(function() { | |
sock = dgram.createSocket(); | |
var l = 8; | |
buf = new Buffer(l); | |
for (var i = 0; i < l; i++) { | |
buf[i] = 100; | |
} | |
log('sending ping...'); | |
sock.addListener('message', function(msg, rinfo) { | |
log('got pong from '+ rinfo.address +":"+ rinfo.port); | |
}); | |
sock.send(8000, "localhost", buf, 0, buf.length); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Boy oh boy, run this if you ever want to see why you should always assign timeouts to a variable, at least