Created
November 13, 2010 20:09
-
-
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.
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
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