Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    });
});
like image 315
extensa5620 Avatar asked Aug 22 '26 16:08

extensa5620


1 Answers

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.
like image 67
user207421 Avatar answered Aug 25 '26 05:08

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!