nodejs:错误:EADDRNOTAVAIL,无法分配请求的地址

时间:2011-07-26 11:50:22

标签: javascript sockets node.js

我在172.16.1.218上运行了以下node.js服务器:

var net=require('net');

var server = net.createServer(function (socket) {
        socket.write("Echo server\r\n");
        socket.pipe(socket);
});
server.listen(6001, "172.16.1.218");

我可以远程登录并按预期回声。

我在172.16.1.224上运行了以下node.js服务器:

var net = require('net');

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

  // Every time someone connects, tell them hello and then close the connection.
  socket.addListener("connect", function () {
    sys.puts("Connection from " + socket.remoteAddress);
    socket.end("Hello World\n");
  });

});

// Fire up the server bound to port 7000 on localhost
server.listen(6001,"172.16.1.218");

但是当我尝试运行它时,我收到以下错误:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EADDRNOTAVAIL, Cannot assign requested address
    at Server._doListen (net.js:1100:5)
    at net.js:1071:14
    at Object.lookup (dns.js:159:5)
    at Server.listen (net.js:1065:20)
    at Object.<anonymous> (/home/hynese/Desktop/test.js:16:8)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)

我关闭了所有防火墙等等。我无法理解这个错误。希望有人可以提供帮助。

非常感谢,

1 个答案:

答案 0 :(得分:13)

172.16.1.224上,您无法收听172.16.1.218,因为这不是您正在收听的机器的IP。

如果您想收听该计算机,请使用:

server.listen(6001,"172.16.1.224");