[zeromq-dev] Chunk by chunk file transfer
Supraja Jayakumar
suprajasankari at gmail.com
Fri Feb 17 12:56:20 CET 2017
Hi.
I am sending an image file from 0mq server to 0mq client in 1024 byte
chunks using pull-push socket. On the client side, i append the chunk to a
string named chunks. It is simply initilaized like var chunks='' and reset
to empty string on every new request. On receiving a 'success' message in
the 'res' socket in the client side, the chunks data structure is written
to a png file.
But on the client side, i see that even before any incoming data is
appended to chunks, the size of chunks is always between 1-20 bytes. Why is
this ?
Also, the incoming message size should be only 1024 bytes. But i get
smaller or larger incoming chunks. Why is this ?
On server side, i do:
size = getFilesizeInbytes(obj.file); // get the size of
the file
buf = fs.readFileSync(obj.file); // get the file in
a buffer
var i=0;var j=i+1024;
while (i < size){ // WHILE(){ the send
loop }
if (j < size){
sendingBuff = buf.toString().substring(i,j);
push.send(sendingBuff);
console.log('Sent file part ' + i + ' to ' + (j-1));
i = i + 1024;
j = i + 1024;
}
else{
j = size;
sendingBuff = buf.toString().substring(i,j);
push.send(sendingBuff);
console.log('Sent file part ' + i + ' to ' + (j-1));
i = size + 1;
rp.send('success');
}
}
}
On client side:
Client.prototype._handleMessageFromPushSocket = function(msg) {
fs.write('img.png', chunks, 'binary', function(err){
if(err) {
console.log(err);
}
console.log('written successfully!');
});
};
On recieving the 'success' message from server on the 'req' socket, we do:
if(response_message=='success') {
fs.writeFile('out.png', chunks, 'binary', function(err) {
if(err) throw err;
console.log('Image written');
});
}
Thank you
Supraja J
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20170217/6cac3a6e/attachment.htm>
More information about the zeromq-dev
mailing list