-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UDP Dgram ERR_SOCKET_CANNOT_SEND #2484
Comments
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 cc @cjihrig I see you added that in nodejs/node@5587ff1ccd9. 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. |
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
|
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 var LOCAL_IP = "XXX.XXX.XX.XX"
var HOST_IP = "XX.XXX.XX.XXX";
var PORT = 45239;
var dgram = require("dgram");
var client = dgram.createSocket("udp4");
//Listen for repsonse locally
client.on("listening", function(){
var adrInfo = client.address();
console.log("Client listening on " + adrInfo.address + ":" + adrInfo.port);
var text = 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 error
client.on('error',function(error){
console.log('Error: ' + error);
client.close();
});
//Binding to given location
client.bind(PORT, LOCAL_IP);
//Close socket after x ms
setTimeout(function(){client.close();},8000); |
When dgram socket implicit binding fails, an attempt is made to clean up the send queue. This was originally implemented using an 'error' handler that performed cleanup and then emitted a fake error, which concealed the original error. This was done to prevent cases where the same error was emitted twice. Now that the errorMonitor event is available, use that to perform the cleanup without impacting the actual error handling. PR-URL: nodejs#31958 Refs: nodejs/help#2484 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This error is no longer used within core. This commit removes it. PR-URL: nodejs#31958 Refs: nodejs/help#2484 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
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
And my error message being:
The text was updated successfully, but these errors were encountered: