Node: Sending UDP packet on timer, infinite loop [duplicate]












1
















This question already has an answer here:




  • Why does a while loop block the event loop?

    5 answers




I'm trying to send a packet from my server to ping a client every few seconds in an infinite loop at all times.



This is the code I am running:



const dgram = require('dgram');
const message = Buffer.from('Some bytes');
const clientPing = dgram.createSocket('udp4');

const pinging = true;

function ping() {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
clientPing.close();
});
}

function sleep(time, callback) {
var stop = new Date().getTime();
while(new Date().getTime() < stop + time) {
;
}
callback();
}

function loop() {
while(pinging == true) {
sleep(3000, function() {
ping();
console.log('ping');
});
}
}

loop();


What's weird is that the console logs the string ping just fine at the given interval but the packet is never sent and never reaches my client. But when I run just ping(); outside of the loop the packet hits the client.



Udp isn't supposed to wait for a response after it sends the packet and doesn't wait for a response. What am I missing here?










share|improve this question













marked as duplicate by jfriend00 node.js
Users with the  node.js badge can single-handedly close node.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    You're blocking the event queue completely. Don't sleep synchronously.

    – tkausl
    Nov 22 '18 at 2:47











  • I'm not sure what you mean. How would you loop this? I've tried it with setTimeout(() => { ping(); }, 3000); and the loop seems to run without waiting for the timeout to end.

    – Eru
    Nov 22 '18 at 2:50











  • node.js runs your Javascript single threaded. So, while you're in sleep() NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging can never change unless it happens directly inside the while loop. No other events will ever get processed. This type of question has been covered multiple times here.

    – jfriend00
    Nov 22 '18 at 3:30


















1
















This question already has an answer here:




  • Why does a while loop block the event loop?

    5 answers




I'm trying to send a packet from my server to ping a client every few seconds in an infinite loop at all times.



This is the code I am running:



const dgram = require('dgram');
const message = Buffer.from('Some bytes');
const clientPing = dgram.createSocket('udp4');

const pinging = true;

function ping() {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
clientPing.close();
});
}

function sleep(time, callback) {
var stop = new Date().getTime();
while(new Date().getTime() < stop + time) {
;
}
callback();
}

function loop() {
while(pinging == true) {
sleep(3000, function() {
ping();
console.log('ping');
});
}
}

loop();


What's weird is that the console logs the string ping just fine at the given interval but the packet is never sent and never reaches my client. But when I run just ping(); outside of the loop the packet hits the client.



Udp isn't supposed to wait for a response after it sends the packet and doesn't wait for a response. What am I missing here?










share|improve this question













marked as duplicate by jfriend00 node.js
Users with the  node.js badge can single-handedly close node.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    You're blocking the event queue completely. Don't sleep synchronously.

    – tkausl
    Nov 22 '18 at 2:47











  • I'm not sure what you mean. How would you loop this? I've tried it with setTimeout(() => { ping(); }, 3000); and the loop seems to run without waiting for the timeout to end.

    – Eru
    Nov 22 '18 at 2:50











  • node.js runs your Javascript single threaded. So, while you're in sleep() NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging can never change unless it happens directly inside the while loop. No other events will ever get processed. This type of question has been covered multiple times here.

    – jfriend00
    Nov 22 '18 at 3:30
















1












1








1









This question already has an answer here:




  • Why does a while loop block the event loop?

    5 answers




I'm trying to send a packet from my server to ping a client every few seconds in an infinite loop at all times.



This is the code I am running:



const dgram = require('dgram');
const message = Buffer.from('Some bytes');
const clientPing = dgram.createSocket('udp4');

const pinging = true;

function ping() {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
clientPing.close();
});
}

function sleep(time, callback) {
var stop = new Date().getTime();
while(new Date().getTime() < stop + time) {
;
}
callback();
}

function loop() {
while(pinging == true) {
sleep(3000, function() {
ping();
console.log('ping');
});
}
}

loop();


What's weird is that the console logs the string ping just fine at the given interval but the packet is never sent and never reaches my client. But when I run just ping(); outside of the loop the packet hits the client.



Udp isn't supposed to wait for a response after it sends the packet and doesn't wait for a response. What am I missing here?










share|improve this question















This question already has an answer here:




  • Why does a while loop block the event loop?

    5 answers




I'm trying to send a packet from my server to ping a client every few seconds in an infinite loop at all times.



This is the code I am running:



const dgram = require('dgram');
const message = Buffer.from('Some bytes');
const clientPing = dgram.createSocket('udp4');

const pinging = true;

function ping() {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
clientPing.close();
});
}

function sleep(time, callback) {
var stop = new Date().getTime();
while(new Date().getTime() < stop + time) {
;
}
callback();
}

function loop() {
while(pinging == true) {
sleep(3000, function() {
ping();
console.log('ping');
});
}
}

loop();


What's weird is that the console logs the string ping just fine at the given interval but the packet is never sent and never reaches my client. But when I run just ping(); outside of the loop the packet hits the client.



Udp isn't supposed to wait for a response after it sends the packet and doesn't wait for a response. What am I missing here?





This question already has an answer here:




  • Why does a while loop block the event loop?

    5 answers








node.js loops udp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:45









EruEru

286




286




marked as duplicate by jfriend00 node.js
Users with the  node.js badge can single-handedly close node.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jfriend00 node.js
Users with the  node.js badge can single-handedly close node.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    You're blocking the event queue completely. Don't sleep synchronously.

    – tkausl
    Nov 22 '18 at 2:47











  • I'm not sure what you mean. How would you loop this? I've tried it with setTimeout(() => { ping(); }, 3000); and the loop seems to run without waiting for the timeout to end.

    – Eru
    Nov 22 '18 at 2:50











  • node.js runs your Javascript single threaded. So, while you're in sleep() NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging can never change unless it happens directly inside the while loop. No other events will ever get processed. This type of question has been covered multiple times here.

    – jfriend00
    Nov 22 '18 at 3:30
















  • 2





    You're blocking the event queue completely. Don't sleep synchronously.

    – tkausl
    Nov 22 '18 at 2:47











  • I'm not sure what you mean. How would you loop this? I've tried it with setTimeout(() => { ping(); }, 3000); and the loop seems to run without waiting for the timeout to end.

    – Eru
    Nov 22 '18 at 2:50











  • node.js runs your Javascript single threaded. So, while you're in sleep() NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging can never change unless it happens directly inside the while loop. No other events will ever get processed. This type of question has been covered multiple times here.

    – jfriend00
    Nov 22 '18 at 3:30










2




2





You're blocking the event queue completely. Don't sleep synchronously.

– tkausl
Nov 22 '18 at 2:47





You're blocking the event queue completely. Don't sleep synchronously.

– tkausl
Nov 22 '18 at 2:47













I'm not sure what you mean. How would you loop this? I've tried it with setTimeout(() => { ping(); }, 3000); and the loop seems to run without waiting for the timeout to end.

– Eru
Nov 22 '18 at 2:50





I'm not sure what you mean. How would you loop this? I've tried it with setTimeout(() => { ping(); }, 3000); and the loop seems to run without waiting for the timeout to end.

– Eru
Nov 22 '18 at 2:50













node.js runs your Javascript single threaded. So, while you're in sleep() NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging can never change unless it happens directly inside the while loop. No other events will ever get processed. This type of question has been covered multiple times here.

– jfriend00
Nov 22 '18 at 3:30







node.js runs your Javascript single threaded. So, while you're in sleep() NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging can never change unless it happens directly inside the while loop. No other events will ever get processed. This type of question has been covered multiple times here.

– jfriend00
Nov 22 '18 at 3:30














2 Answers
2






active

oldest

votes


















1














As @tkausl says, you're blocking the event loop. Don't use that sleep() function. Try something like this instead:



function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}

loop();





share|improve this answer
























  • I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

    – Eru
    Nov 22 '18 at 3:23



















0














So I finally got it behave properly using this:



function loop() {

setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);

}

loop();


So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.






share|improve this answer
























  • There's no point in loop in this case. Just use setInterval directly.

    – Brad
    Nov 22 '18 at 3:31











  • I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

    – Eru
    Nov 22 '18 at 3:39


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














As @tkausl says, you're blocking the event loop. Don't use that sleep() function. Try something like this instead:



function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}

loop();





share|improve this answer
























  • I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

    – Eru
    Nov 22 '18 at 3:23
















1














As @tkausl says, you're blocking the event loop. Don't use that sleep() function. Try something like this instead:



function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}

loop();





share|improve this answer
























  • I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

    – Eru
    Nov 22 '18 at 3:23














1












1








1







As @tkausl says, you're blocking the event loop. Don't use that sleep() function. Try something like this instead:



function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}

loop();





share|improve this answer













As @tkausl says, you're blocking the event loop. Don't use that sleep() function. Try something like this instead:



function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}

loop();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 2:56









BradBrad

116k28236396




116k28236396













  • I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

    – Eru
    Nov 22 '18 at 3:23



















  • I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

    – Eru
    Nov 22 '18 at 3:23

















I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

– Eru
Nov 22 '18 at 3:23





I first had to comment out clientPing.close(); in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.

– Eru
Nov 22 '18 at 3:23













0














So I finally got it behave properly using this:



function loop() {

setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);

}

loop();


So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.






share|improve this answer
























  • There's no point in loop in this case. Just use setInterval directly.

    – Brad
    Nov 22 '18 at 3:31











  • I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

    – Eru
    Nov 22 '18 at 3:39
















0














So I finally got it behave properly using this:



function loop() {

setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);

}

loop();


So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.






share|improve this answer
























  • There's no point in loop in this case. Just use setInterval directly.

    – Brad
    Nov 22 '18 at 3:31











  • I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

    – Eru
    Nov 22 '18 at 3:39














0












0








0







So I finally got it behave properly using this:



function loop() {

setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);

}

loop();


So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.






share|improve this answer













So I finally got it behave properly using this:



function loop() {

setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);

}

loop();


So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 3:27









EruEru

286




286













  • There's no point in loop in this case. Just use setInterval directly.

    – Brad
    Nov 22 '18 at 3:31











  • I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

    – Eru
    Nov 22 '18 at 3:39



















  • There's no point in loop in this case. Just use setInterval directly.

    – Brad
    Nov 22 '18 at 3:31











  • I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

    – Eru
    Nov 22 '18 at 3:39

















There's no point in loop in this case. Just use setInterval directly.

– Brad
Nov 22 '18 at 3:31





There's no point in loop in this case. Just use setInterval directly.

– Brad
Nov 22 '18 at 3:31













I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

– Eru
Nov 22 '18 at 3:39





I wanted to use it as an export from this module that my server file can import. So I did const loop = funtcion() { setInterval(() => {}, 3000); } in the end.

– Eru
Nov 22 '18 at 3:39



這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini