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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6829563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:00:05  来源:igfitidea点击:

nodejs: Error: EADDRNOTAVAIL, Cannot assign requested address

javascriptsocketsnode.js

提问by Eamorr

I have the following node.js server running on 172.16.1.218:

我在 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");

I can telnet into it and it echos as expected.

我可以 telnet 进入它,它按预期回响。

I have the following node.js server running on 172.16.1.224:

我在 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");

But when I try to run it, I get the following error:

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

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)

I've turned off all firewalls, etc. I can't make any sense of this error. Hoping someone can help.

我已经关闭了所有防火墙等。我无法理解这个错误。希望有人能帮忙。

Many thanks in advance,

提前谢谢了,

回答by pimvdb

On 172.16.1.224you cannot listen on 172.16.1.218because that's not the IP of the machine you're listening on.

172.16.1.224你可以不听的172.16.1.218,因为这不是机器你在听的IP。

If you want to listen on that machine, use:

如果您想在该机器上收听,请使用:

server.listen(6001,"172.16.1.224");