Sending and receiving a UDP broadcast to and from 255.255.255.255 in Node.js - node.js

I'm trying to implement a BOOTP server in Node, for which broadcasting is a necessity. Sadly the docs are a little bit confusing and I'm getting weird errors all the way. Funny enough, the errors are different on Windows 7 and Ubuntu.
Did someone actually manage to send a UDP broadcast to 255.255.255.255 or receive one under this address?
Could someone provide me a simple Node UDP broadcasting demo?

Using punt I tried to bind a connection to 255.255.255.255 on port 5000 and I get this error EADDRNOTAVAIL
I think the address it too general. See this link
Here is the code, which is just a slightly modified version of a punt example.
var punt = require('punt');
var server = punt.bind('255.255.255.255:5000');
var a = punt.connect('255.255.255.255:5000');
server.on('message', function(msg){
console.log(msg);
});
setInterval(function(){
a.send({ hello: 'world' });
}, 150);
which yields this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: bind EADDRNOTAVAIL
at errnoException (dgram.js:439:11)
at dgram.js:206:28
at dns.js:72:18
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:901:3

Related

why node js socket not working when change port to https?

Normally in app.js i use this code on port 3000 it's work good (on my-domain.com:3000).
.
http.listen(3000, function(){
console.log('start server on port :3000');
});
then i want to use on https, so i change app.js to
http.listen(443, function(){
console.log('start server on port :443');
});
When run node app.js , it's show error
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::443
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at Server._listen2 (net.js:1252:14)
at listen (net.js:1288:10)
at Server.listen (net.js:1384:5)
at Object.<anonymous> (/home/admin/web/my-domain.com/public_html/app.js:28:6)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
Normally i have to access to my-domain.com:3000 for user chat. So i want to know how can i access to https://www.my-domain.com for user chat ?
You have the following error message:
Error: listen EADDRINUSE :::443
[...]
This message means that the port 443 is currently in use by some process.
You can check which process is actually using the said port by executing one of the many tools for network checking (such as netstat for Windows, lsof or netstat on Linux).
Refer to the manual for those tools to achieve the correct result, based upon your operating system.

Everytime I run cluster.fork(), I get a Error: bind EADDRINUSE

I'm using node.js, and using the cluster module. Everytime I run cluster.fork(), I always get a
throw er; // Unhandled 'error' event
Error: bind EADDRINUSE
at exports._errnoException (util.js:746:11)
at cb (net.js:1205:33)
at rr (cluster.js:592:14)
at Worker.<anonymous> (cluster.js:563:9)
at process.<anonymous> (cluster.js:692:8)
at process.emit (events.js:129:20)
at handleMessage (child_process.js:324:10)
at Pipe.channel.onread (child_process.js:352:11)
I've been googling this, and I have no idea how this is happening because I'm not passing in any port numbers.
Thanks
EDIT: Posting code
var setupWorkers = function() {
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < 5; i++) {
cluster.fork();
}
}
and this is a function that is called in the app.js which I run by calling node app.js
I was starting a server more than once with all the threads so the port was bound already
The stack trace you provide indicates that EADDRINUSE is coming from the net module. EADDRINUSE typically means that you are trying to listen on an IP/port combination more than once. So, for example, if this is a clustered web server, perhaps all your workers are trying to bind to port 80 on the same IP address. Without more code, it's impossible to tell what's happening.
The example code you gave in the subsequent comment does not trigger EADDRINUSE for me. Instead it errors with cluster.fork is not a function because there's no check for cluster.isMaster before calling cluster.fork().

Getting ECONNREFUSED error in nodejs when logstash server disconnects

I am using node-bunyan and bunyan-logstash-tcp in my nodejs application to send the logs to logstash (1.4.2) and elasticsearch (1.4.2).
Whenever the logstash server disconnects or is not reachable, my nodejs application crashes giving the following error
ERROR
-------
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
bunyan-logstash-tcp should actually handle this error.
Can anyone please help me to solve this nodejs crashing issue.
I was able to figure out the issue.
Error event needs to be handled while creating tcp bunyan stream
stream: bunyantcp.createStream({
host: '127.0.0.1',
port: 9998
}).on('error', console.log)
This is not mentioned in the bunyan-logstash-tcp documentation, but was there in an example code.
UPDATE: Sample configuration
this.log = bunyan.createLogger({
name: name,
streams: [
{
level: 'error',
type: 'raw',
serializers: bunyan.stdSerializers,
stream: bunyantcp.createStream({
application: process.title,
max_connect_retries: 10, // Don't give up on reconnecting
retry_interval: 1000 * 60 // Wait 1s between reconnect attempts
}).on('error', console.log)
}
],
level: 'debug'
});
For logstash you could probably switch to UDP from TCP.
There are few important advantages:
no connection issues (no reconnect problems, etc)
less performance overhead
less network overhead
Logentries' got a nice blogpost about it: https://blog.logentries.com/2014/07/tcp-or-udp-for-logging/
Take a look at some out-of-the box libs for logstash (elk stack) that support UDP: https://github.com/devmetrics/devmetrics-nodejs-core

Nodejs: Listen on port 80 after using setuid/setgid

I just got convinced by some internet articles, that using setuid/setgid to switch to a lower privileged user might be important. Since I am developing a web app, I decided to go for www-data.
So I am using the userid NPM module to figure out the user and group ID of www-data, and then change to it. However, when I do that - and it doesn't matter where entirely - I get the following (in this example, the security handler was executed at the very, very bottom of the code):
2014-09-04T23:07:05.812Z - info: BIRD3 Security -> Changed to www-data:www-data (33:33)
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
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
As you can see, as soon as I change my privilege level, it drops the accessibility to port 80 too.
Is there a way how I can implement security but keep using port 80 without using something like this?
You don't need a third-party module to do that. process.setgid() and process.setuid() both accept either an ID or a groupname/username.
Also make sure you are dropping the privileges AFTER listening on port 80 and that you call process.setgid() before process.setuid().
Example:
var net = require('net');
var srv = net.createServer(function(s) {
});
srv.listen(24, function() {
console.log('listening');
});
process.setgid('www-data');
process.setuid('www-data');
Isn't the setgid() and setuid() supposed to run after connecting, within the callback response?
srv.listen(24, function() {
console.log('listening');
process.setgid('www-data');
process.setuid('www-data');
});

tutorial of node.js code sample fails with Error: listen EADDRINUSE

I am a beginner programmer that is trying to learn node.js using the following tutorial site
http://www.nodebeginner.org/#hello-world
I got to the point where I was trying to set up the server but got an error with the below code
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1022:14)
at listen (net.js:1044:10)
at Server.listen (net.js:1110:5)
at Object.<anonymous> (/Users/.........../server.js:7:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
any help would be appreciated
EADDRINUSE means that address is in use.
Basically, you tried to start two servers at the same time that both use port 8888. You have to stop or kill one before starting another. The other server on port 8888 could be another process running your node script, or it could be something else in the system that serves content on port 8888.
Alternatively, you can get this if you don't let the socket settle for a few seconds after terminating the old server.
A more practical answer based on this great one.
Find out what is using port 8888 with this command:
lsof -i tcp:8888
You should get something like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 86456 myName 13u IPv6 0xa6b50fb47c9c3c81 0t0 TCP *:ddi-tcp-1 (LISTEN)
Now that you know which process is in the way, KILL IT! Softly, like so:
kill -15 86456

Resources