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.
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
Discussion
View additional discussion.