Skip to content

Instantly share code, notes, and snippets.

@jrgm
Last active December 11, 2015 22:39
Show Gist options
  • Save jrgm/4671535 to your computer and use it in GitHub Desktop.
Save jrgm/4671535 to your computer and use it in GitHub Desktop.
Exercise the local ephemeral port range and see if it's exhausted
// Exercise the local ephemeral port range and see if it's exhausted
// usage: `while [ 1 ]; do node ~/grab-a-socket.js; sleep 1; done`
var net = require('net');
var options = {
port: 63300,
host: '127.0.0.1'
};
var connectListener = function() {
var elapsed = Date.now() - startTime;
console.log(new Date().toISOString(), 'socket connected',
socket.address(), socket.remoteAddress, socket.remotePort,
elapsed + 'ms');
socket.end();
};
var startTime = Date.now();
var socket = net.connect(options, connectListener);
socket.on('data', function(data) {
var elapsed = Date.now() - startTime;
console.log(new Date().toISOString(), 'data', data.toString(), elapsed + 'ms');
socket.end();
});
socket.on('error', function(error) {
var elapsed = Date.now() - startTime;
console.log(new Date().toISOString(), 'Error:', error.toString(), elapsed + 'ms');
socket.end();
});
socket.on('end', function() {
var elapsed = Date.now() - startTime;
console.log(new Date().toISOString(), 'socket disconnected', elapsed + 'ms');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment