It seems that NodeJS imposes limitation on localAddress of net.connect method. Although the destination hosts are different, NodeJS requires the localAddres to be unique. As far as I know, in outgoing TCP connections, only the combination of source IP, source port, destination IP, and destination port needs to be unique. However, NodeJS APIs don't provide any way to create more than one connection with the same source port, unless they have different source IP addresses. I have created a source code only to show the limitation. In fact I want to create a proxy server, and this server needs to be connected to more than 65K servers. Of course they are not unique. But NodeJS limits my concurrent connections to less than 65K as it does not let to reuse localPort and therefor will make it impossible to have more than 65K concurrent outgoing TCP connections. Is there any way to bypass this limitation?
const duplicatePort=1010;
var m=net.connect({port:80,host:'host1.com',localPort:duplicatePort},function()
{
m.on('data',function(data){
console.log('host1: ',data.toString());
})
var n=net.connect({port:80,host:'host2.com',localPort:duplicatePort},function()
{
n.on('data',function(data){
console.log('host2: ',data.toString());
})
m.write('GET / HTTP/1.1\r\nHost: www.host1.com\r\n\r\n');
m.write('GET / HTTP/1.1\r\nHost: www.host2.com\r\n\r\n');
})
})
It will throw the following error. (Second call to the net.connect function)
Error: bind EADDRINUSE 0.0.0.0:1010