Join 3,427 readers in helping fund MetaFilter (Hide)


I need to write a Node.js UDP Echo Server
May 28, 2014 1:24 PM   Subscribe

I need to write a UDP echo server in Node.js, but I'm having trouble parsing the available tutorial, could use some expert help.

Thing is, I'm a total Node.js noob - this is my first time using it. I come from a PHP background. A very kind person has already solved my problem in PHP and posted it here on the web.

This works perfectly, whatever I send gets echoed back to me with an OK, making it easy for the client to confirm receipt of the datagram.

The only tutorial for Node.js that comes close is this one, but I can't figure out how to make the server echo what it receives back to the client, or how to make the client display that echo.

Help me socket jockeys, you're my only hope.

By the way, I can add any module you like, and already have socket.io, so that's the environment I'm in.
posted by signsofrain to Computers & Internet (5 answers total) 1 user marked this as a favorite
 
I too am a total NodeJS noob, and I'm just skimming here and don't have time to really test my results, but from the example code you linked to, couldn't you just copy the send code from the UDP client into the server receive message, but replace:

client.send(message, 0, message.length, PORT, HOST, function(err, bytes) ...

with

client.send(message, 0, message.length, remote.port, remote.address, function(err, bytes) ...

? Every time you get a message, you send that message back out to the port and address you got it from...
posted by straw at 1:53 PM on May 28


straw's got the same approach i have, listed out below:
//server.js
var PORT = 33333;
var HOST = '127.0.0.1';

var dgram = require('dgram');
var server = dgram.createSocket('udp4');

//As soon as the server is ready to receive messages, handle it with this handler
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});

//When getting a message, handle it like so:
server.on('message', function (message, remote) {
//print out the message
console.log(remote.address + ':' + remote.port +' - ' + message);
//prepare a response
var okBuffer = new Buffer("OK " + message);
server.send(okBuffer, 0, okBuffer.length, remote.port, remote.address, function(err, bytes){
if (err){
throw err;
}
//print out to the server's console that we've successfully sent the response to the client
console.log("OK sent to client");
});
});

server.bind(PORT, HOST);

//client.js
var PORT = 33333;
var HOST = '127.0.0.1';

var dgram = require('dgram');
var message = new Buffer('My KungFu is Good!');

var client = dgram.createSocket('udp4');

//When the client socket receives the message event, handle it with this handler
client.on('message', function(message, remote){
console.log("Received from server: " + message);
});

//Send a message [message variable] to the host and port configured in the first two lines,
//logging as appropriate.
client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
});
posted by jangie at 1:54 PM on May 28 [1 favorite]


This SO question and answer may be helpful and seems to be based on your example.
posted by paulash at 1:57 PM on May 28


Thank you jangle. Cut, paste, works like a charm. Now I can focus on the meat of my project. Damn I love the hivemind.
posted by signsofrain at 3:50 PM on May 28


Be aware that in node.js, uncaught errors will stop the server process - to prevent the app ending up in an unknown state - and you won't then be listening for any other new messages until you start the node app again. You can cope with this for unforseen errors by using forever to restart the process automagically in production, but it's best to deal with errors properly where you can so code is less brittle.

With callbacks, it's easiest to deal with the error at source if it won't impact application state, i.e.
server.send(okBuffer, 0, okBuffer.length, remote.port, remote.address, function(err, bytes){
if (err) {
console.error("Error sending OK buffer to client", err);
} else {
//print out to the server's console that we've successfully sent the response to the client
console.log("OK sent to client");
});
For more complex code where you want to divorce the error handler from the main code, to make sure your state is always known, you can use 'throw err' inside a try...catch block, or emit your own error event which you listen for in your logging/handling code, or wrap the thing in a domain in newer versions of node.js.
posted by ArkhanJG at 2:42 AM on May 29 [1 favorite]


« Older My brother did something racis...   |  I'm about to try a generic equ... Newer »

You are not logged in, either login or create an account to post comments