简体   繁体   中英

Node.js websocket error "Error: listen EADDRNOTAVAIL Error: listen EADDRNOTAVAIL"

Application work fine on localhost .but when its connect to server it getting error.
I connect server through port 22

This is the error

Error: listen EADDRNOTAVAIL Error: listen EADDRNOTAVAIL
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at net.js:1146:9
at dns.js:72:18
at process._tickCallback (node.js:419:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:906:3

Any Help !!!

-update-

i run netstat -tulpn | grep 22 netstat -tulpn | grep 22

Result:

 tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      683/sshd
 tcp6       0      0 :::22                   :::*                    LISTEN      683/sshd

but i run netstat -tulpn | grep 80 netstat -tulpn | grep 80

Nothing Display.

Any Help.is this server fault?

running netstat -nlt

     Active Internet connections (only servers)
     Proto Recv-Q Send-Q Local Address           Foreign Address         State
     tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN
     tcp        0      0 127.0.0.1:5901          0.0.0.0:*               LISTEN
     tcp        0      0 127.0.0.1:5902          0.0.0.0:*               LISTEN
     tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN
     tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN
     tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
     tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
     tcp6       0      0 :::22                   :::*                    LISTEN
     tcp6       0      0 ::1:631                 :::*                    LISTEN

Running netstat -anp | grep :80 netstat -anp | grep :80

     tcp        1      0 162.243.145.226:60728   91.189.94.25:80         CLOSE_WAIT  1726/ubuntu-geoip-p
     tcp        1      0 162.243.145.226:47842   74.125.239.148:80       CLOSE_WAIT  8104/epiphany-brows
     tcp        1      0 162.243.145.226:60727   91.189.94.25:80         CLOSE_WAIT  1417/ubuntu-geoip-p
     tcp        1      0 162.243.145.226:58818   198.41.30.199:80        CLOSE_WAIT  8104/epiphany-brows

You are using a used port. You must change a port or you must kill a process which is listening on a port. Open terminal and write (example): lsof -i :22 or lsof -i :80 or lsof -i :8000 and kill PID of the process.


How to change the listening PORT in total.js?

  1. in /app/config or /app/config-release or /app/config-debug :
default-ip       : 127.0.0.1
default-port     : 8000

or

// For e.g. Heroku
default-ip       : auto
default-port     : auto
  1. if exist files: release.js or debug.js :
var fs = require("fs");
var options = {};

// options.ip = "127.0.0.1";
// options.port = parseInt(process.argv[2]);
options.port = 8000;
  1. if exists only index.js
// for development:
require('total.js').http('debug', { port: 8000 });

// or for production:
require('total.js').http('release', { port: 8000 });

Thanks and documentation: http://docs.totaljs.com

Just saw this today. User had commented out 127.0.0.1 localhost in their /etc/hosts file, and another network service was resolving localhost to a different IP address not associated with the user's machine.

Solution was to add this line to /etc/hosts

127.0.0.1 localhost

I think you are using a Unix based OS, if that is the case then you can't use any port below 1024 without sudo access.

Also before digging too deep check that the listening port is not being used by any other process.

A quick fix (for development only):

  • sudo node file.js or
  • server.listen(3000); // any number > 1024

For production (never run node on production with root access.)

you have to map the listening port (ex:3000) to 80 using ip tables

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

Port 22 is reserved for ssh and on the list of well known ports, please check http://en.wikipedia.org/wiki/Port_(computer_networking)#Common_port_numbers

I would recommend you to run node apps for development on ports 8000-9000, but you can use any from the registered ports range.

If you are only reaching this error after many requests, you might want to check your port range settings and adjust them. There is also TCP timeout settings, such as FIN_WAIT that can keep connections open for longer than expected.

sysctl -a | grep net sysctl -a | grep net should show all network connection settings.

sysctl -a | grep port sysctl -a | grep port for port range settings.

sysctl -a | grep keep sysctl -a | grep keep for tcp wait settings.

See here and here (mac here ) for a little more detail/advice on what the settings are.

If you are going to modify settings, I would suggest that you first copy /etc/sysctl.conf to something like /etc/sysctl.conf.bak and then modify the setting in that file. They can be adjusted via the command-line but that is temporary and resetting them can be a mess.

You probably don't want to do this in production.

Additional docs:

https://ma.ttias.be/linux-increase-ip_local_port_range-tcp-port-range/

https://easyengine.io/tutorials/linux/sysctl-conf/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM