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

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,

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

Related

Why doesn't Node.js throw an error when it can't connect to socket?

When i call both of these functions only the upper one fails and returns an error
const { Socket } = require("net")
class Client {
connect () {
this.a = new Socket()
this.a.connect(this.port, this.host)
this.socket = new Socket()
this.socket.connect(this.port, this.host)
}
}
The problem is, that i know that the port this should connect to, is not used. Both functions should throw an error.
If i call the lower one first, still the one with this.a fails.
If i use this.socket for both, the first one always fails even if i change the order of them.
To differentiate between them i used a different port to connect to but also unused.
this.socket = new Socket()
this.socket.connect(6743, this.host)
this.a = new Socket()
this.a.connect(6744, this.host)
The this.port and this.host variables are not the problem, because if run the script while the server on the port is online it works.
Error Message that should be thrown:
events.js:291
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6744
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6744
}```
Like other users have mentioned, the second error is not being triggered because the application effectively stops running after the first error. To prove this, you could try putting a console.log between your two attempts to connect to a socket and see that won't run as expected either.
In nodeJs some module throw error in event
Socket throw error in event.on('error') and you should checked event
I hope below link help you:
https://stackoverflow.com/a/25847067/6759368

Handling an error on a tcp connection using the "net" module of NodeJS

Im trying to learn the "net" module of NodeJS, but i have run into an annoying problem. My code is in 2 scripts.
//index.js
var net = require("net")
var server = net.createServer(socket=>{
socket.write("Hello!")
})
server.on("error", err=>{
console.error(err)
})
server.listen(50001)
and
//client
var net = require("net")
net.createConnection({
host:"localhost",
port: 50001
})
.on("data", data=>{
console.log(data.toString())
})
when i run the 2 scripts...
node index.js
node client.js
...the message "Hello!" is sent to the client with no problems. If i CTRL+C out of the client (to simulate an unexpected closure), i get the following error
events.js:174
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
which i find strange, as I have registered an "error" event on the server. What am i doing wrong?
The program registers a handler for an 'error' event on the server's listening socket, but it does not register an 'error' handler on the separate new socket that is created to carry the connection that is made by the client. (Every connection to the server gets a separate new socket, distinct from the listening socket and from any other connections that have been made earlier.)
That new connection socket is the one that is passed as the argument to the server's connect handler. It's the socket that you write 'Hello!' into.
If you want the program to be notified of an error on that new socket then you must register an 'error' handler on the new socket. Something like this:
var net = require("net")
var server = net.createServer(socket=>{
socket.on('error', err=>{
console.error('connected socket: ' + err);
});
socket.write("Hello!")
})
server.on("error", err=>{
console.error(err)
})
server.listen(50001)
Similarly, if you wanted the server to read data from the connection then you would register a handler for the 'data' event on the new connection socket, in the same way as you already did on the client's socket.

How to subscribe to broadcast address in node.js (255.255.255.255)

I cannot listen to messages being broadcast on 255.255.255.255. The ArtNet protocol docs say that ArtNet controllers broadcast onto 255.255.255.255 on port 6454 (see also here.
When I try to listen to 255.255.255.255 using node.js's dgram module, I get an "EINVAL" errror, because 255.255.255.255 is an invalid address.
I've tried subscribing to 192.255.255.255, 192.168.255.255, and 192.168.10.255 (instead of 255.255.255.255). None of these work either. Tested on Windows and Ubuntu.
const dgram = require('dgram');
const sock = dgram.createSocket('udp4', (msg, peer) => {
console.log(msg, peer);
});
sock.bind(6454, () => {
sock.addMembership('255.255.255.255');
});
Stacktrace:
dgram.js:581
throw errnoException(err, 'addMembership');
^
Error: addMembership EINVAL
at Socket.addMembership (dgram.js:581:11)
at Socket.sock.bind (/home/kyle/temptest/listen3.js:15:8)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at startListening (dgram.js:128:10)
at state.handle.lookup (dgram.js:249:7)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
socket.addMembership() is used to enroll a socket as a destination for multicast traffic. Your use case here is broadcast, not multicast, so don't try to use addMembership() at all.
You shouldn't need to do anything special to receive broadcast traffic. Just bind() your socket to the appropriate port (in this case 6454) as you are already doing, and then your socket's message-event callback should fire whenever a broadcast to that port is received.
If you want to write a test program to exercise your listener by sending broadcast traffic to port 6454 then that test program will need to call setBroadcast() on its dgram socket before trying to send() to port 6454 at the 255.255.255.255 broadcast address.
Error: addMembership EINVAL
at Socket.addMembership (dgram.js:581:11)
...
EINVAL - means Error: Invalid Value.
You cannot bind socket to any broadcast address as stated in number of RFCs.

Check if UDP socket is runing on a certain port, close it, then run it again

I am working with UDP sockets, I am facing a certain issue that when a code is run for the first time it works, but when it is run for the second time it gives me this error:
at _handle.lookup (dgram.js:266:18)
at _combinedTickCallback (internal/process/next_tick.js:142:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
I deduced that this error is issued because the port is still in use, so I am trying to work on a sample code that checks if a socket is running on a certain port, if yes close it and then create the socket again on the same port.
Here is the sample code:
var PORT = 7777;
var HOST = '10.0.1.10';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
gener(server, PORT, HOST);
function gener(sock, prt, hst){
sock.close();
sock.bind(prt, hst);
}
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
When I run it, it gives me the following error:
dgram.js:638
throw new errors.Error('ERR_SOCKET_DGRAM_NOT_RUNNING');
^
Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running
at Socket._healthCheck (dgram.js:638:11)
at Socket.bind (dgram.js:186:8)
at gener (/home/caracall/Desktop/server.js:11:18)
at Object.<anonymous> (/home/caracall/Desktop/server.js:7:1)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
This is the issue:
function gener(sock, prt, hst){
sock.close();
sock.bind(prt, hst);
}
You definitely can not close a socket and then expect "bind" to succeed immediately afterwards. You need to create new socket. You probably want something closer to this:
function gener(sock, prt, hst){
if (sock) {
sock.close();
sock = null;
}
sock = dgram.createSocket('udp4');
sock.bind(prt, hst);
}
You are attempting to bind with a closed socket, that's why Error ERR_SOCKET_DGRAM_NOT_RUNNING is thrown. Please refer to the Node.js document:
If binding fails, an 'error' event is generated. In rare case (e.g. attempting to bind with a closed socket), an Error may be thrown.
To check whether a certain port is occupied, and kill the owner process (dangerous operation), you can use kill-port:
const kill = require('kill-port');
kill(7777);

binaryjs client crashes when server restarts

I'm using binaryjs to implement a video transfer program between node.js server and node-webkit client.The client stays connected,once a video is uploaded,the client starts downloading it.
It works fine generally,the client does get the videos.But the client throws an error and crashes when the server restarts or crashes. I have been listening the BinaryClient 'error' & 'close' event,however it doesnot works.
I guess maybe i'd listen 'error' event from something else.What to do to fix the problem?Anyone can help?
Thanks a lot!
app.js:
var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app).listen(3000);
var binaryServer = new BinaryServer({ server: server, path: '/binary' });
binaryServer.on('connection', function (client) {
// client on stream
// client on close
// client on error
});
// binaryServer on error
client:
var BinaryClient = require('binaryjs').BinaryClient;
var binaryClient = new BinaryClient('ws://127.0.0.1:3000/binary');
binaryClient.on('open', function () {
// binaryClient.createStream( ... )
});
// binaryClient on stream
// binaryClient on close
// binaryClient on error
error:
Uncaught node.js Error
Error: read ECONNRESET
at exports._errnoException (util.js:742:11)
at TCP.onread (net.js:541:26)

Categories

Resources