Last active
April 18, 2016 05:15
-
-
Save eplawless/041bf70cedd8f70db3248e9733373cb1 to your computer and use it in GitHub Desktop.
Node's TCP sockets hardcode SO_REUSEADDR by default and this cannot be changed
This file contains hidden or 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
Using IPv4 unspecified address 0.0.0.0 | |
Using IPv6 unspecified address :: | |
Interface lo0 has address ::1 | |
Interface lo0 has address 127.0.0.1 | |
Interface lo0 has address fe80::1%lo0 | |
Interface en0 has address 192.168.1.7 | |
Starting too many servers on port 4567... | |
Listening on 0.0.0.0 port 4567 | |
Listening on :: port 4567 | |
Listening on ::1 port 4567 | |
Listening on 127.0.0.1 port 4567 | |
Listening on fe80::1%lo0 port 4567 | |
Listening on 192.168.1.7 port 4567 | |
Started 6 servers | |
Connecting to 0.0.0.0 port 4567 | |
Connecting to :: port 4567 | |
Connecting to ::1 port 4567 | |
Connecting to 127.0.0.1 port 4567 | |
Connecting to fe80::1%lo0 port 4567 | |
Connecting to 192.168.1.7 port 4567 | |
Got connection on ::1 port 4567 | |
Got connection on ::1 port 4567 | |
Got connection on 127.0.0.1 port 4567 | |
Got connection on 127.0.0.1 port 4567 | |
Got connection on fe80::1%lo0 port 4567 | |
Got connection on 192.168.1.7 port 4567 | |
Message on ::1 port 4567: "hello ::" | |
Message on ::1 port 4567: "hello ::1" | |
Message on 127.0.0.1 port 4567: "hello 0.0.0.0" | |
Message on 127.0.0.1 port 4567: "hello 127.0.0.1" | |
Message on fe80::1%lo0 port 4567: "hello fe80::1%lo0" | |
Message on 192.168.1.7 port 4567: "hello 192.168.1.7" |
This file contains hidden or 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
#!/usr/bin/env node | |
var net = require('net'); | |
var os = require('os'); | |
var interfaces = os.networkInterfaces(); | |
var hosts = ['0.0.0.0', '::']; | |
Object.keys(interfaces).forEach(function(interfaceName) { | |
var interface = interfaces[interfaceName]; | |
interface.forEach(function(info) { | |
var address = info.address; | |
// append the interface name onto link-local IPv6 addresses | |
// http://stackoverflow.com/a/34275391 | |
if (address.indexOf('fe80:') === 0) { | |
address += '%' + interfaceName; | |
} | |
console.log('Interface ' + interfaceName + ' has address ' + address); | |
hosts.push(address); | |
}); | |
}); | |
var port = 4567; | |
function createServer(host) { | |
return new Promise(function(resolve, reject) { | |
var server = net.createServer(); | |
server.listen({ host: host, port: port }, function() { | |
console.log('Listening on ' + host + ' port ' + port); | |
server.on('connection', function(socket) { | |
console.log('Got connection on ' + host + ' port ' + port); | |
var buffer = []; | |
socket.on('data', function(data) { | |
buffer.push(data); | |
}); | |
socket.on('end', function() { | |
console.log('Message on ' + host + ' port ' + port + ':', | |
JSON.stringify(buffer.join(''))); | |
}); | |
}); | |
resolve(server); | |
}); | |
server.on('error', reject); | |
}); | |
} | |
function connectClient(host) { | |
return new Promise(function(resolve, reject) { | |
console.log('Connecting to ' + host + ' port ' + port); | |
var socket = net.connect(port, host, function() { | |
socket.end('hello ' + host); | |
resolve(socket); | |
}); | |
socket.on('error', reject); | |
}); | |
} | |
console.log('Starting too many servers on port ' + port + '...'); | |
Promise.all(hosts.map(createServer)) | |
.then(function(servers) { | |
console.log('Started ' + servers.length + ' servers'); | |
return Promise.all(hosts.map(connectClient)); | |
}) | |
.catch(function(error) { | |
console.error('ERROR:', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment