我编写应用程序通过nodejs发送文件多播我逐块读取文件并像这样以块的形式发送
for (var block = 1; block <= number_of_block; block++) {
sendBlock(FILEPATH, block)
通过 CHUNK 函数 sendBlock
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);
});
});
我创建客户端来接收消息
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)
});
});
}
});
但是当服务器发送超过 1000 条消息时,客户端无法捕捉到所有服务器发送
block -- 6907
block -- 6908
block -- 6909
block -- 6910
block -- 6911
block -- 6912
block -- 6913
客户接收并写入
block ------ 1008
block ------ 1009
block ------ 1010
block ------ 1011
我测试接收的最大文件是 10.4 MB。
如何接收来自发送方的所有数据?