Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS run tcp server on multiple ports?

Tags:

node.js

Is it possible to run a tcp server in nodejs on multiple ports?

var socketServer = net.createServer(function (socket) {


    socket.on('end', function () {


    });

    socket.on('error', function (error) {

    });     

    return socket;
}).listen(8080);

Any ideas how the above could also run on port 8888?

like image 628
user3490755 Avatar asked May 26 '14 14:05

user3490755


1 Answers

The best you can do is extract your connection handler and create multiple server instances:

function connHandler(socket) {
  socket.on('end', function() {

  });

  socket.on('error', function(error) {

  });
}

net.createServer(connHandler).listen(8080);
net.createServer(connHandler).listen(8088);
like image 135
mscdex Avatar answered Oct 28 '22 14:10

mscdex