Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created November 13, 2010 20:09
Show Gist options
  • Save TooTallNate/675597 to your computer and use it in GitHub Desktop.
Save TooTallNate/675597 to your computer and use it in GitHub Desktop.
Dumps individual TCP 'requests' to files. I use this for creating test files when I'm implementing protocols in Node.
var port = 8080;
require('net').createServer(function(stream) {
var id = Date.now();
var filename = stream.remoteAddress + '.' + id + '.dump';
var dumpStream = require('fs').createWriteStream(filename);
dumpStream.on('close', function() {
console.error('File "' + filename + '" closed');
});
stream.on('timeout', function() {
console.error('Closing connection id ' + id + ', due to inactivity (timeout after 5 seconds)');
stream.end();
});
stream.pipe(dumpStream);
console.error('Connection from "' + stream.remoteAddress + ':' + stream.remotePort + '" (id ' + id + '), piping contents to "' + filename + '"');
stream.setTimeout(5000);
}).listen(port, function() {
console.error('TCP server listening on port: ' + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment