先贴出 client 和 server 的代码,node 的版本号为 v8.1.2
服务端代码:
// buffer_server.js
const fs = require('fs');
const http = require('http');
const zlib = require('zlib');
const server = http.createServer((req, res) => {
let index = 0;
req.on('data', function(chunk) {
console.log(++index, chunk.length);
});
res.end('OK');
})
server.listen(3001, () => {
console.log('started');
});
客户端代码:
// buffer_client.js
const fs = require('fs');
const http = require('http');
const zlib = require('zlib');
const path = require('path');
const filename = process.argv[2];
const server = process.argv[3];
const options = {
port: 3001,
hostname: server,
method: 'PUT',
protocol: 'http:',
headers: {
filename: path.basename(filename),
'Content-Encoding': 'gzib'
}
}
const req = http.request(options, (res) => {
console.log('client done');
})
fs.readFile(filename, (err, buffer) => {
console.log('获取文件完毕\n');
console.log('开始压缩');
zlib.gzip(buffer, (err, buffer) => {
console.log('压缩完毕');
console.log(buffer.length); // 1336191186 1.2G 左右的文件
req.write(new Buffer(1336191186));
req.end();
})
})
req.on('error', (err) => {
console.log(err);
/**
*
* 出现的错误:
*
* { Error: read ECONNRESET
at exports._errnoException (util.js:1016:11)
at TCP.onread (net.js:609:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
*/
})
req.on('data', (data) => {
console.log(data, 'data');
})
```
问题是,在上传 1.2GB 左右的压缩文件,一次性去通过 write 去发送给 server 之后,client 会报错,报错信息如下:
```
{ Error: read ECONNRESET
at exports._errnoException (util.js:1016:11)
at TCP.onread (net.js:609:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
```
我自己判定是因为一次性 write 的 buffer 数量太大造成的,求解具体原因 谢谢
1
ljcarsenal 2017-11-23 00:22:18 +08:00 via Android
虽然不知道怎么写 但直觉告诉我应该用 stream
|
2
hxsf 2017-11-23 07:54:43 +08:00 via iPhone
服务器端 提前断开了
应该在 req.on('end' 再去 res.send |
3
wingerwang OP @hxsf 应该在流全部到达之后,犯了个小错误 谢谢!
|