Node.js trying to connect tcp

When there is a massive tcp connection from the node.js javascript client to the server, when restarting the server, some connection attempts hang. I wrote a simple script to reproduce it:

var net = require('net');

function Conn() {
    var conn = new net.Socket({allowHalfOpen: false});
    conn.setTimeout(1000);
    conn.on('error', function (connectionException) {
        console.log('TCP Repeater error: ' + connectionException);
        this.connected = false;
    });
    conn.on('connect', function () {
        console.log("connected");
        this.connected = true;
    });
    conn.on('close', function () {
        console.log("connection closed");
        this.connected = false;
    });
    conn.on('timeout', function () {
        console.log("connection timeout");
        this.connected = false;
    });
    conn.connect(9997, "localhost");
}

for (var i=0;i<400;i++) {
    new Conn();
}

      

Run this script with the start of the tcp server. Some attempts get errors because the server was started, some attempts are connected after the server started. However, some attempts will not trigger any events and will just hang.

Is there anyway I can kill these suspension attempts? It looks like the connection timeout will not help as it concerns the inactivity of the established connections. Is there a way to set the connection timeout like setSoTimeout in java?

thank

+3


source to share





All Articles