I write application to send file multicast by nodejs I read file by chunk and send it in block like this
for (var block = 1; block <= number_of_block; block++) {
sendBlock(FILEPATH, block)
function sendBlock by CHUNK
function sendBlock(file, block) {
fs.open(file, 'r', function(err, fp) {
if (err) {
return;
}
var buf = new Buffer(4 + CHUNK_SIZE);
fs.read(fp, buf, 4, CHUNK_SIZE, (block - 1) * CHUNK_SIZE, function(err, bytesRead) {
if (err) {
}
buf[0] = 0;
buf[1] = opcodes.OPCODE_DATA;
buf[2] = (block >> 8) & 0xFF;
buf[3] = block & 0xFF;
udpserver.send(buf, 0, 4 + bytesRead, PORT, MULTICAST_IP_ADDRESS);
fs.close(fp);
});
});
I create client to receive message
fs.open(fileName, 'a', function(e, id) {
if (4 + CHUNK_SIZE > message.length) {
fs.write(fd, message, 4, message.length - 4, (block - 1) * CHUNK_SIZE, function() {
fs.close(fd, function() {
console.log('file closed', block);
send("miss block:" + missArray);
});
});
} else {
console.log("message length:", message.length)
console.log((block - 1) * CHUNK_SIZE)
fs.write(fd, message, 4, CHUNK_SIZE, (block - 1) * CHUNK_SIZE, function() {
fs.close(fd, function() {
console.log('1file closed', block);
if (block % NUMBER_BLOCK == 0) {
if (blockArray.length > 0) {
missArray = missArray.concat(blockArray);
}
blockArray = range(block + 1, NUMBER_BLOCK)
}
//udpserver.send(block+1)
});
});
}
});
But when server send over 1000 message client can not catch it all server send
block -- 6907
block -- 6908
block -- 6909
block -- 6910
block -- 6911
block -- 6912
block -- 6913
client reveive and write
block ------ 1008
block ------ 1009
block ------ 1010
block ------ 1011
And I test the maximum file to receive is 10.4 MB.
How to receive all data from sender?
Node.js is subject to the limitations of the underlying operating system. The operating system puts limits on the number of outstanding handles that can be concurrently held by a process.
It's likely that you're exhausting the number of available file descriptors. I recommend using connection pooling to reduce the number of file descriptors that your applications attempt to consume. So instead of trying to send 1000 things at once, limit your program to a pool of say 100 connections at a time.
There are several connection pooling libraries available through npm - one popular choice is poolr.