通过代理使用流在tcp套接字上发送文件

时间:2021-01-08 07:38:00

What i tried to achieve with node.js/io.js, is to send a file from one server to another one via a proxy. To avoid memory buffering i want to use streams.

我试图用node.js / io.js实现的是通过代理将文件从一个服务器发送到另一个服务器。为了避免内存缓冲,我想使用流。

The proxy should be able to connect to multiple targets dynamically. The target connection information for the proxy should be send prior to the filedata.

代理应该能够动态连接到多个目标。应该在filedata之前发送代理的目标连接信息。

With normal socket communication and buffering it is not a problem. But how or in general can this be done with streams??

通过正常的套接字通信和缓冲,这不是问题。但是如何或一般可以用流完成?

var net = require('net');
var fs = require('fs');

//create readstream from file
var myFile = fs.createReadStream('E:/sample.tar.gz');

// Proxy server
//####################################################################################################

var proxy = net.createServer(function (socket) {

    // Create a new connection to the TCP server
   var client = net.connect('9010'); 
   // 2-way pipe between client and TCP server
   socket.pipe(client).pipe(socket);

}).listen(9000);


// Targetserver
//####################################################################################################

var server = net.createServer(function (socket) {

    // create filestream to write data into file
    var destfile = fs.createWriteStream('E:/sample_copy.tar.gz')

    socket.on('data', function (buffer) {

        console.log('Get data on targetserver...');
        // write buffer to file    
        destfile.write(buffer);

    });

    socket.on('end', function () {
        // release file from writestream
        destfile.end();
     });

}).listen(9010);


// Client
//####################################################################################################

// Send file to proxy
var client = new net.Socket();

// connect to proxy
client.connect('9000', '127.0.0.1', function () {

    console.log('Connection to proxy opened');

});

// send data to proxy
myFile.pipe(client);

// read response from taget
client.on('data', function(data) {

    console.log('Response: ' + data);
    // close the client socket completely
    client.destroy();

});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection to proxy closed');
});

Any hint to a good tutorial is also welcome.

任何暗示好的教程也是受欢迎的。

TMOE

1 个解决方案

#1


socket.write() already uses streams under the hood so you don't need to do anything special. Just send it the usual Buffer object or string and it will use a stream.

socket.write()已经在引擎盖下使用了流,所以你不需要做任何特别的事情。只需向它发送通常的Buffer对象或字符串,它就会使用一个流。

From the current source code of io.js, here's what happens when you use socket.write():

从当前的io.js源代码中,可以看出使用socket.write()时会发生什么:

Socket.prototype.write = function(chunk, encoding, cb) {
  if (typeof chunk !== 'string' && !(chunk instanceof Buffer))
    throw new TypeError('invalid data');
  return stream.Duplex.prototype.write.apply(this, arguments);
};

And stream is declared like this:

并且流声明如下:

const stream = require('stream');

Apologies if I've misunderstood your question/requirements! By all means, clarify if I have misunderstood you and I'll try again (or delete this answer so it's not a distraction).

如果我误解了你的问题/要求,请道歉!通过各种方式,澄清我是否误解了你,我会再试一次(或删除这个答案,这不是分心)。

#1


socket.write() already uses streams under the hood so you don't need to do anything special. Just send it the usual Buffer object or string and it will use a stream.

socket.write()已经在引擎盖下使用了流,所以你不需要做任何特别的事情。只需向它发送通常的Buffer对象或字符串,它就会使用一个流。

From the current source code of io.js, here's what happens when you use socket.write():

从当前的io.js源代码中,可以看出使用socket.write()时会发生什么:

Socket.prototype.write = function(chunk, encoding, cb) {
  if (typeof chunk !== 'string' && !(chunk instanceof Buffer))
    throw new TypeError('invalid data');
  return stream.Duplex.prototype.write.apply(this, arguments);
};

And stream is declared like this:

并且流声明如下:

const stream = require('stream');

Apologies if I've misunderstood your question/requirements! By all means, clarify if I have misunderstood you and I'll try again (or delete this answer so it's not a distraction).

如果我误解了你的问题/要求,请道歉!通过各种方式,澄清我是否误解了你,我会再试一次(或删除这个答案,这不是分心)。