Skip to content

Instantly share code, notes, and snippets.

@felixge felixge/log
Created May 29, 2012

Embed
What would you like to do?
$ node udp_server.js
# start udp_client.js in another terminal
0.00 mb/sec (0.00 mb total)
0.00 mb/sec (0.00 mb total)
42.48 mb/sec (42.48 mb total)
140.32 mb/sec (182.80 mb total)
129.50 mb/sec (312.30 mb total)
141.91 mb/sec (454.22 mb total)
136.52 mb/sec (590.74 mb total)
112.98 mb/sec (703.72 mb total)
0.00 mb/sec (703.72 mb total)
0.00 mb/sec (703.72 mb total)
0.00 mb/sec (703.72 mb total)
var dgram = require('dgram');
var totalSize = 1024 * 1024 * 1024;
var messageSize = 8 * 1024;
var bytesSent = 0;
var host = process.argv[2] || '127.0.0.1';
var client = dgram.createSocket("udp4");
function sendNext() {
if (bytesSent >= totalSize) {
console.log('done sending');
return;
}
var message = new Buffer(messageSize);
client.send(message, 0, message.length, 8081, host, sendNext);
bytesSent += message.length;
}
sendNext();
var bytesTotal = 1024 * 1024 * 1024;
var bytesReceived = 0;
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
bytesReceived += msg.length;
});
server.on("listening", function () {
var address = server.address();
console.log("server listening " + address.address + ":" + address.port);
});
server.bind(8081);
var lastBytesReceived = 0;
setInterval(function() {
var totalReceived = (bytesReceived / 1024 / 1024).toFixed(2);
var speed = ((bytesReceived - lastBytesReceived) / 1024 / 1024).toFixed(2);
console.log('%s mb/sec (%s mb total)', speed, totalReceived);
lastBytesReceived = bytesReceived;
if (bytesTotal === bytesReceived) process.exit(0);
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.