You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I've been trying to write a script that can send a message to a server (which I've hidden), which upon receiving, returns a response which I'm trying to listen for.
I seem to be having trouble with .bind(), but without it I'm listening to 0.0.0.0;, if you have any ideas, please let me know!
thanks
constdgram=require('dgram');constbuffer=require("buffer");constserver=dgram.createSocket('udp4');varitem="Message";varhostIP="XX.XXX.XX.XXX";varhostPort=45239;server.bind({address: hostIP,port: hostPort,exclusive: true});server.on("connect",function(){console.log("Connected");});server.on("message",function(message,info){console.log(info.address+':'+info.port+' - '+message);});server.on("listening",function(){varinfo=server.address();console.log("UDP Server listening to "+info.address+":"+info.port);});server.send(Buffer.from(item),hostPort,hostIP,function(error){if(error){client.close();}else{console.log("Data sent");}});
And my error message being:
events.js:288
throw er; // Unhandled 'error' event
^
Error [ERR_SOCKET_CANNOT_SEND]: Unable to send data
�[90m at Socket.onListenError (dgram.js:525:22)�[39m
�[90m at Object.onceWrapper (events.js:418:26)�[39m
�[90m at Socket.emit (events.js:311:20)�[39m
�[90m at dgram.js:341:14�[39m
�[90m at processTicksAndRejections (internal/process/task_queues.js:85:21)�[39m
Emitted 'error' event on Socket instance at:
�[90m at Socket.onListenError (dgram.js:525:8)�[39m
�[90m at Object.onceWrapper (events.js:418:26)�[39m
[... lines matching original stack trace ...]
�[90m at processTicksAndRejections (internal/process/task_queues.js:85:21)�[39m {
code: �[32m'ERR_SOCKET_CANNOT_SEND'�[39m
}
You're trying to send data before the socket has been bound. Node tries to do that implicitly for you but the operation fails, presumably because either the address is already in use or because the bind address is wrong.
Either way, you should get a more legible error when you move the server.send(...) call inside your server.on("listening", ...) event listener. Right now, that ERR_SOCKET_CANNOT_SEND is masquerading the real error.
cc @cjihrig I see you added that in nodejs/node@5587ff1. It might be better to forward the original listen error?
That commit is over three years old, so I don't recall if I had any reason for implementing it that way, but it makes sense now to forward the original error.
You're trying to send data before the socket has been bound. Node tries to do that implicitly for you but the operation fails, presumably because either the address is already in use or because the bind address is wrong.
Either way, you should get a more legible error when you move the server.send(...) call inside your server.on("listening", ...) event listener. Right now, that ERR_SOCKET_CANNOT_SEND is masquerading the real error.
Thanks, I've tried nesting the event calls, and adding the connect event, which no-longer throws an error. When a successful connection occurs, the correct address is given, but it doesn't listen to the same ip/port.
As well as throwing an error on sending a message, which I couldn't find a reference to.
const dgram = require('dgram');
const buffer = require("buffer");
const server = dgram.createSocket('udp4');
var item = "Message";
var hostIP = "127.0.0.1";
var hostPort = 45239;
server.connect(hostPort,hostIP,function(){
var info = server.address();
console.log("Connected to " + info.address + ":" + info.port);
});
server.on("message", function(message, info){
console.log(info.address + ':' + info.port +' - ' + message);
});
server.on("listening", function(){
var info = server.address();
console.log("Server listening to " + info.address + ":" + info.port);
server.send(Buffer.from(item),hostPort,hostIP,function(error){
if (error){
console.log(error);
server.close();
}else{
console.log("Data sent");
}
});
});
Output
Server listening to 0.0.0.0:52870
Connected to 127.0.0.1:52870
Error: send EISCONN 127.0.0.1:45239
at doSend (dgram.js:697:16)
at defaultTriggerAsyncIdScope (internal/async_hooks.js:311:12)
at afterDns (dgram.js:643:5)
at processTicksAndRejections (internal/process/task_queues.js:85:21) {
errno: 'EISCONN',
code: 'EISCONN',
syscall: 'send',
address: '127.0.0.1',
port: 45239
}
After trial and error, I ended up with this working script, sending a piece of data to a destination, and listening for its response. Where LOCAL_IP would normally be 192.168.X.Y , and HOST_IP being the destination IP.
varLOCAL_IP="XXX.XXX.XX.XX"varHOST_IP="XX.XXX.XX.XXX";varPORT=45239;vardgram=require("dgram");varclient=dgram.createSocket("udp4");//Listen for repsonse locallyclient.on("listening",function(){varadrInfo=client.address();console.log("Client listening on "+adrInfo.address+":"+adrInfo.port);vartext=Buffer.from("Message");client.send(text,PORT,HOST_IP,function(error){if(error){console.log(error);}else{console.log("Sent "+text);}});client.on("message",function(message,remote){console.log("Recieved response as: "+message+" from "+remote.address+":"+remote.port);});});//On error event display errorclient.on('error',function(error){console.log('Error: '+error);client.close();});//Binding to given locationclient.bind(PORT,LOCAL_IP);//Close socket after x mssetTimeout(function(){client.close();},8000);
Activity
bnoordhuis commentedon Feb 25, 2020
You're trying to send data before the socket has been bound. Node tries to do that implicitly for you but the operation fails, presumably because either the address is already in use or because the bind address is wrong.
Either way, you should get a more legible error when you move the
server.send(...)call inside yourserver.on("listening", ...)event listener. Right now, thatERR_SOCKET_CANNOT_SENDis masquerading the real error.cc @cjihrig I see you added that in nodejs/node@5587ff1ccd9. It might be better to forward the original listen error?
cjihrig commentedon Feb 25, 2020
That commit is over three years old, so I don't recall if I had any reason for implementing it that way, but it makes sense now to forward the original error.
Luuccc commentedon Feb 25, 2020
Thanks, I've tried nesting the event calls, and adding the connect event, which no-longer throws an error. When a successful connection occurs, the correct address is given, but it doesn't listen to the same ip/port.
As well as throwing an error on sending a message, which I couldn't find a reference to.
Output
Luuccc commentedon Feb 27, 2020
After trial and error, I ended up with this working script, sending a piece of data to a destination, and listening for its response. Where
LOCAL_IPwould normally be 192.168.X.Y , and HOST_IP being the destination IP.dgram: don't hide implicit bind errors
errors: remove unused ERR_SOCKET_CANNOT_SEND error