gpt4 book ai didi

node.js - 如何在 Nodejs 中一次性通过 TCP 发送文件

转载 作者:可可西里 更新时间:2023-11-01 02:45:11 28 4
gpt4 key购买 nike

我想使用 Nodejs 通过 tcp/ip 发送文件,我成功地处理了大小小于 65536 字节的文件。

Do you set buffersize? Yes, I do.

我尝试发送具有 233565 字节的图片,但看起来 nodejs 将我的文件分开发送多次。

这是我的示例代码

服务器.js

const net = require('net');
net.bytesWritten = 300000;
net.bufferSize = 300000;
const fs = require('fs');
const server = net.createServer((c) => {
// 'connection' listener
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
// c.write('hello\r\n');
fs.readFile('daemon0.png' , (err, data) =>{
if(!err){
console.log(data.length);
c.write(data);
}
else {
console.log('readfile daemon0 err');
}
});
c.pipe(c);
});
server.on('error', (err) => {
throw err;
});
server.listen(8124, 'localhost', () => {
console.log('server bound');
});

Server.js 输出

server bound

client connected

233565

client disconnected

客户端.js

const net = require('net');
net.bufferSize = 300000;
net.bytesRead = 300000;
const client = net.connect({port: 8124, address: 'localhost' }, () => {
// 'connect' listener
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
// console.log(data.toString());
console.log(net.bufferSize,data.length);
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});

Client.js 输出

connected to server!

300000 5840

300000 65536

300000 65536

300000 65536

300000 31125

disconnected from server

作为客户端的两个输出,客户端似乎有接收数据的限制,最大为 65536 字节。如何在 TCP nodejs 中扩展缓冲区大小限制?

我在 nodejs gitter chat 中问这个问题和 mpotra回答这个对我来说有意义的解释。

That's not a problem. That's how streaming works: sending something large into smaller packets. If your only desire is to increase the buffer size so that you would receive the file into one single data event, then you're thinking of it wrong. If your file would have 500MB, based on your expectation, both the server and the client would have to allocated 500MB! into memory, just to satisfy the need of one single data event; which is obviously the wrong approach to data transfer.

最佳答案

Node 正在发送 整个 Buffer 就好了,但是您的客户端将以 block 的形式发出数据事件。如果你想接收整个文件,你需要缓冲 block :

const chunks = []
client.on('data', chunk => chunks.push(chunk))
client.on('end', () => {
const file = Buffer.concat(chunks)
// do what you want with it
})

我不知道在 Node 中控制何时发出数据事件的方法。

关于node.js - 如何在 Nodejs 中一次性通过 TCP 发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com