Skip to content

Instantly share code, notes, and snippets.

@trevnorris trevnorris/udp_client.js
Last active Apr 18, 2017

Embed
What would you like to do?
'use strict';
Error.stackTraceLimit = Infinity;
const dgram = require('dgram');
const print = process._rawDebug;
// Problem doesn't exist with localhost, so plug in a Google server's IP.
const HOST = '172.217.5.110';
const client = dgram.createSocket('udp4');
const message = Buffer.alloc((1 << 16) - 29);
let cntr = 0;
(function runner() {
client.send(message, 0, message.length, 41234, HOST, (err) => {
if (err) {
print('client errored');
throw err;
}
if (++cntr >= 100) {
print(`sent ${cntr} messages`);
cntr = 0;
}
//setTimeout(runner, 60);
setImmediate(runner);
});
})();
'use strict';
Error.stackTraceLimit = Infinity;
const dgram = require('dgram');
const print = process._rawDebug;
// Problem doesn't exist with localhost, so plug in a Google server's IP.
const HOST = '172.217.5.110';
const client = dgram.createSocket('udp4');
// If this is too large then use the value of 1472.
const message = Buffer.alloc((1 << 16) - 29);
let cntr = 0;
(function runner() {
const client = dgram.createSocket('udp4');
client.send(message, 0, message.length, 41234, HOST, (err) => {
if (err) {
print('client errored');
throw err;
}
if (++cntr >= 100) {
print(`send ${cntr} messages`);
cntr = 0;
}
client.close();
setImmediate(runner);
});
})();
'use strict';
const dgram = require('dgram');
const print = process._rawDebug;
const msg = 'abcdefghijklm'.repeat(5039); // 2^16 - 29
const client = dgram.createSocket('udp4');
let cntr = 0;
setInterval(function() {
for (var i = 0; i < 100; i++) {
if (++cntr > 8000) {
clearInterval(this);
client.close();
print(`${process.memoryUsage().rss / 1024 >>> 0} kB rss`);
return;
}
if (cntr % 100 === 0) print(`sent ${cntr}`);
client.send(msg, 41234, '8.8.8.8');
}
}, 1);
'use strict';
const dgram = require('dgram');
const print = process._rawDebug;
const server = dgram.createSocket('udp4');
let bytes_received = 0;
let cntr = 0;
server.on('error', (err) => {
print('server errored');
throw err;
});
server.on('message', (data, rinfo) => {
if (++cntr >= 100) {
print(`received ${cntr} messages`);
cntr = 0;
}
bytes_received += data.length;
});
server.on('listening', () => {
print(`listening on ${server.address().address}:${server.address().port}`);
});
server.bind(41234, '0.0.0.0');
(function runner(t) {
setTimeout(() => {
t = process.hrtime(t);
const kB_sec = bytes_received / (t[0] + t[1] / 1e9) / 3 / 1024;
print(`${kB_sec.toFixed(2)} kB/sec`);
bytes_received = 0;
runner(process.hrtime());
}, 3000);
})(process.hrtime());
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.