Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NodeJS TCP Server, onData small chunks

NodeJS TCP Server, onData small chunks

Problem

I made a simple NodeJS TCP server which a Java Client sends an image:

encodedImage = 
out.write("IMG;" + encodedImage);
out.flush();

My NodeJS server is as follows:

net.createServer(function(TCPSocket){
        TCPSocket.on("data", function(data){
            console.log("TCP Recieved: " + data.length);    
        });
}).listen(5000);

However, even if I send all data and flush it immediately, the output is as follows:

TCP Recieved: 13
TCP Recieved: 1344
TCP Recieved: 1344
TCP Recieved: 1344
TCP Recieved: 1344
TCP Recieved: 1344
TCP Recieved: 1472
TCP Recieved: 1344
TCP Recieved: 1344
TCP Recieved: 1344
TCP Recieved: 1344

I want it to recieve it in a simple chunk, but I assume it is happenng due to NodeJS's event handling mechanism, what is the best way to achieve single chunk of data for data sent as a single chunk? As far as I know, a TCP window can be much larger than 1344 bytes of data. I thought of using header values as HTTP so I know the length ad construct the object I want.

Problem courtesy of: Mustafa

Solution

You're correct, in Node the data will come in chunked. You'll need to keep a buffer, concatenating all the blocks that come in and then when the socket is closed, write your data out to the file system. Here is an example that was able to receive an image:

net = require('net');
fs = require('fs');

net.createServer(function(socket){
  var buffer = new Buffer(0, 'binary');

  socket.on("data", function(data){
    buffer = Buffer.concat([buffer, new Buffer(data,'binary')]);
  });

  socket.on("end", function(data) {
    fs.writeFile("image.jpg", buffer, function(err) {
      if(err) {
        console.log(err);
      } else {
        console.log("Socket[" + socket.name + "] closed, wrote data out to sinfo.data");
      }
    }); 

  });

}).listen(5000);

console.log('ready');

I sent an image to it using netcat:

$ netcat localhost 5000 
Solution courtesy of: Kyle Burton

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

NodeJS TCP Server, onData small chunks

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×