extensa5620
extensa5620

Reputation: 699

nodejs - How to test whether remote socket is open

I am creating a socket pass through inspector.

Basically, I start up a socket server (net.createServer) and a socket client (net.connect). For testing purposes, I do not have a endpoint socket waiting.

I want test whether the endpoint socket is available. If not, nodejs should wait until socket is available.

var net = require('net');
var inbound = net.createServer();
var outbound = net.connect({
    port: 8193
});

inbound.listen(8192, function () { //'listening' listener
    address = inbound.address();
    console.log('Server started on %j', address);
});

inbound.on('connection', function (insock, outbound) {
    console.log('CONNECTED ' + insock.remoteAddress + ':' + insock.remotePort);
    insock.on('data', function (data, outbound) {
        outbound.write(data);
        console.log('DATA ' + data);
    });
});

Upvotes: 3

Views: 3426

Answers (1)

user207421
user207421

Reputation: 310913

The best way to test whether any resource is available is to try to use it. Pre-testing is liable to a number of objections:

  1. If it tests something different from the actual usage, it may yield an incorrect answer.
  2. If it tests the same things as the actual usage it is merely wastefully redundant.
  3. The situation may change between the test and the usage.

Upvotes: 1

Related Questions