Skip to content

Instantly share code, notes, and snippets.

@matt-
Created Dec 28, 2011
Embed
What would you like to do?
Solaris in Node JS
var net = require('net');
var host = 'apache.org';
var max = 300;
var connections = Array();
var count = 0;
var ip = '';
var dns = require('dns');
// the DNS lookups in the socket actually take up file handles.
// lets DNS once then just use the IP from then on.
dns.resolve4(host, function (err, addresses) {
if (err) throw err;
ip = addresses[0];
start();
});
function start(){
console.log(ip);
for(i=0; i<max; i++){
makeConnection();
}
}
function makeConnection(){
var client;
client = net.createConnection(80, ip);
client.setKeepAlive(enable=true, 2000);
// Make the request only send part of the headers
/*
client.on('connect', function(connection) {
console.log('connect');
this.write("GET / HTTP/1.1\r\n");
this.write("Host: "+host+"\r\n");
//this.write("\r\n"); // never send this last header
});
*/
// Send all of the headers but not the POST data
client.on('connect', function(connection) {
console.log('connect');
this.write("POST / HTTP/1.1\r\n");
this.write("Host: "+host+"\r\n");
this.write("Content-Length: 1337\r\n");
//this.write("\r\n");
//this.write("\r\n");
});
client.on('data', function(data) {
console.log('Data: ', data.toString());
});
client.on('end', function() {
console.log("Connection end");
});
client.on('error', function(err) {
console.log("Error: ", err);
});
client.on('close', function(had_error) {
console.log("Connection closed");
makeConnection(); // start up a new one if one dies
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment