joyent/node — Issue #2836: Cluster Listens on Random Port if Port Is Already Bound
Author: benmmurphy
Created: 2012-02-27 15:10:15+00:00
State: open

var cluster = require('cluster');
var net = require('net');

if (cluster.isWorker) {
var server = new net.Server();
server.on("listening", function() {
console.log("listening!");
});

server.on("error", function() {
console.log("error!");
});

server.listen(3000);
} else {
cluster.fork();
}

output when something is already bound to port 3000:

$ node cluster_test.js&
[1] 46332
$ listening! { port: 62932, family: 2, address: '0.0.0.0' }

output when nothing is bound to port 3000:

ntest node cluster_test.js&
[1] 46358
ntest listening! { port: 3000, family: 2, address: '0.0.0.0' }