Node.js进程不检测端口已被使用

时间:2015-09-02 17:02:23

标签: javascript node.js asynchronous process port

我正在尝试使用两个不同的Node进程(使用Cluster)尝试成为端口的服务器。但是,每当第二个进程到达端口时,它都不会检测到端口正在被使用。

我怀疑他们没有检测到端口是否打开的原因是由于回调的性质(我检测端口是否使用或不使用portInUse函数,所以它是异步获取的,并可能在以后引起某种类型的冲突。)

以下是代码:

var cluster = require('cluster');
var net = require('net');

var PORT = 1337;
var list = {};
var portIsBeingUsed = false;

// Variable that detects if the port is in use.
var portInUse = function(port, callback) {
  var server = net.createServer(function(socket) {
    socket.write('Echo server\r\n');
    socket.pipe(socket);
  });

  server.listen(port, 'localhost');
    server.on('error', function (e) {
    callback(true);
  });

    server.on('listening', function (e) {
  server.close();
  callback(false);
    });
};

if (cluster.isMaster) {
  for (var i = 0; i < 2; i++) {
    cluster.fork();
  }

   Object.keys(cluster.workers).forEach(function(id) {
    console.log("I am running with ID : "+ cluster.workers[id].process.pid);
    list[cluster.workers[id].process.pid] = 0;
  });

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
}  else { // Rest of the logic with all Processes goes here.

      // Get the Process ID of the current process in execution.
      var pid = cluster.worker.process.pid;
      console.log("This is process " + pid + " working now.\n");

      // Verify if Port is being used. 
      portInUse(PORT, function(returnValue) {
        if(returnValue) { // Become a Client to the Server
          console.log("port " + PORT + " is being used.\n\n");
          becomeClient(pid);
        } else { // Become a Server
          console.log("port" + PORT + " is not being used.\n\n");
          becomeServer(pid);
        }
      });
}

function becomeServer(pid) {
  var server = list[pid];

  server = net.createServer(function (socket) {
    socket.write('Hello Server 1\r\n');
    socket.end("hello");
    console.log("Someone connected to Server 1. \n");
    socket.pipe(socket);
  });

  server.listen(PORT, function(){
    console.log("Process " + pid + " has become the Server on Port " + PORT);
  });

  server.on("error", function() {
    console.log("there was an error on Process " + pid);
    console.log("this error was becoming a Server.");
  });
}

function becomeClient(pid) {
  var client = list[pid];

  client = net.connect({port: PORT}, function() {
    list[pid].write("I am connected to the port and my pid is " + pid);
  });

  client.on('data', function(data) {
    console.log(data.toString());
    list[pid].end();
  });

  client.on('end', function() {
    console.log('disconnected from server');
  });
}

这是输出:

enter image description here

所以第一个进程(在这种情况下为Process 9120)成为端口1337上的服务器,但是第二个进程没有检测到端口正在被使用,并且某种程度上也变成了服务器(我希望EADDRINUSE在这里,不确定为什么它没有显示任何错误)。

对于为什么这不起作用的任何帮助或澄清将不胜感激。

谢谢,

2 个答案:

答案 0 :(得分:1)

这是预期的行为,它是cluster模块的工作方式(默认情况下)。它允许来自同一端口的传入请求可以轻松地分配给可用的工作人员。

答案 1 :(得分:1)

cluster的很大一部分涉及端口共享,因此您可以让多个工作人员轮流提供请求。工作进程只是从主服务器请求一个端口,实际打开端口,然后主服务器请求返回给请求该端口的任何工作者。