2

I'm just getting started with node.js and UDP. I'm trying to capture UDP packets and then format the output.

The UDP packet's I'm receiving are being split up in to multiple messages. I can't figure out how to reassemble the message. I can concatenate each message, but how do I know that the message is complete? I need to process that data but I need to complete message.

FYI... This is for a scoreboard type application. The statistics are being broadcast via UDP and I'm trying to create an application that will monitor the stats.

Here is some basic code

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

server.on("message", function (msg, rinfo) {
STATS = STATS + msg;
msg = msg + '<!>';
console.log(msg);
  });

// *****************************
// When the message is complete
// Process STATS 
// *****************************

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " +
      address.address + ":" + address.port);
});

server.bind(10101);
// server listening 0.0.0.0:41234

1 Answers1

0

Do you have control over the client? If so, you can simply have some sort of symbol that identifies the end of a message, and your program can detect if msg ends with that.

But UDP sockets are volatile - there is no guarantee that any message will actually arrive. There is also no guarantee that your datagrams will arrive in the order that you send them. Correcting for these issues is more complex - if you don't need the real-time requirement, then switching to TCP is much easier.

TCP will automatically split your data into chunks when you send it from the client, so you will need to use that same end-of-message system, or detect when the socket is closed.

frr171
  • 211
  • 1
  • 7
  • I can't change the data being sent. Is it possible that there is information in the message that's not being shown when displaying on the console? Something that could help putting the info back together? – chrissabato Jul 03 '13 at 00:20