Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

javascript - nodejs: Error: EADDRNOTAVAIL, Cannot assign requested address

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

var net=require('net');

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

I can telnet into it and it echos as expected.

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

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
");
  });

});

// 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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

If you want to listen on that machine, use:

server.listen(6001,"172.16.1.224");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...