Ad

How To Maintained Network Connection In Node.js For Unlimited Requests

- 1 answer

I want to make connection for unlimited times in Nodejs. For example, i write something on some server and after writing on server, server send me response of error (as expected from server) and disconnect. But i want to again make a connection to that server and again want to send request with different parameters. I am not sure where and what logic/code to be put in my following segment of code , so that i can make unlimited requests.

var net = require('net');
var HOST = '40.14.121.178'
var PORT = 12537;
var byteToSend = [0x56, 0x34, ...]

var client = new net.Socket();
client.connect(PORT, HOST, function() {
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
   client.write(byteToSend);
});

client.on('data', function(data) { 
    console.log('DATA: ' + data);
    client.destroy(); 

});

client.on('close', function() {
    console.log('Connection closed');
});

EDITED: actually, i want to make another connection upon disconnect like following style (which is i think wrong)

client.on('close', function() {
    console.log('Connection closed');
client.connect(PORT, HOST, function() {
        console.log('again CONNECTED TO: ' + HOST + ':' + PORT);
        client.write(byteToSend);
    });
});

above re connection raise following error.

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:78:25)
    at writeGeneric (internal/stream_base_commons.js:73:3)
    at Socket._writeGeneric (net.js:713:5)
    at Socket._write (net.js:725:8)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Socket.Writable.write (_stream_writable.js:299:11)
    at bitflipping (C:\Users\...\Desktop\myScripts.js:130:8)
    at Socket.<anonymous> (C:\Users\...\Desktop\myScripts.js:104:9)
    at Object.onceWrapper (events.js:286:20)
Emitted 'error' event at:
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at onwriteError (_stream_writable.js:430:5)
    at onwrite (_stream_writable.js:461:5)
    at _destroy (internal/streams/destroy.js:49:7)
    at Socket._destroy (net.js:613:3)
    at Socket.destroy (internal/streams/destroy.js:37:8)
    at afterWriteDispatched (internal/stream_base_commons.js:78:17)
    at writeGeneric (internal/stream_base_commons.js:73:3)
    at Socket._writeGeneric (net.js:713:5)
    at Socket._write (net.js:725:8)
Ad

Answer

I don't think you can re-use the existing client connection to connect again. Therefore, you'll want to wrap it all in a nice closure/function that you can call again to create a new socket and connect.

Try something like this:

var net = require('net');
var HOST = '40.14.121.178'
var PORT = 12537;
var byteToSend = [0x56, 0x34, ...]

function connect() {

  var client = new net.Socket();
  client.connect(PORT, HOST, function() {
      console.log('CONNECTED TO: ' + HOST + ':' + PORT);
     client.write(byteToSend);
  });

  client.on('data', function(data) { 
      console.log('DATA: ' + data);
      client.destroy(); 

  });

  client.on('close', function() {
      console.log('Connection closed');
      connect();
  });
}

connect();
Ad
source: stackoverflow.com
Ad