nodejs: Error: EADDRNOTAVAIL, Cannot assign requested address

17,413

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");
Share:
17,413
Eamorr
Author by

Eamorr

Updated on July 02, 2022

Comments

  • Eamorr
    Eamorr almost 2 years

    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\r\n");
            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\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,

  • pimvdb
    pimvdb almost 13 years
    @Eamorr: What happens if you assign .224 on the .224 machine and then connect to .224? Is the .218 working correctly?
  • Eamorr
    Eamorr almost 13 years
    Hi, sorry for late reply - I was on my lunch ;) But if I listen on .224 and .218, the messages still don't come through... All I'm trying to do is set up a communication link between two servers...
  • Eamorr
    Eamorr almost 13 years
    I tried assigning .224 on the .224 machine and then connect to .224, but it didn't work either...
  • pimvdb
    pimvdb almost 13 years
    @Eamorr: So if I understand correctly, .218 works and .224 does not work?
  • Eamorr
    Eamorr almost 13 years
    Hey, thanks for all your help. No, nothing works... ;( All I want to do is connect two servers together and pass messages between them ;( I can telnet into one of the servers, but I can't connect them using Node.js
  • pimvdb
    pimvdb almost 13 years
    @Earnorr: If I execute your second code block with my own IP and then navigate to http://ip:6001/ I get the text Hello World. Does this not work for you?
  • Eamorr
    Eamorr almost 13 years
    That works alright, it's just I'm trying to get two servers to communicate with each other via Node.js. I'm not doing any web dev stuff.
  • pimvdb
    pimvdb almost 13 years
    @Earnorr: If you'd like a simple means of passing messages between servers, please have a look at gist.github.com/1106779.
  • Eamorr
    Eamorr almost 13 years
    You absolute legend! It works!!! I had my head in my hands there just before you replied. Greetings from Ireland. Thanks so much!