Node.js TCP转发器

时间:2011-02-28 13:37:49

标签: tcp node.js

我有以下简单的NodeJS脚本,并希望稍微修改它....

var sys = require( 'sys' ), net = require( 'net' );

var outputserver = net.createServer( function( stream ) {
    stream.addListener( 'data', function( data ) {
        sys.puts( data );
        //Want to output anything from the clientserver data here
    });
}).listen( 7001, 'localhost' );

var clientserver = net.createServer( function( stream ) {
    stream.addListener( 'data', function( data ) {
        sys.puts( data );

    });
}).listen( 7000, 'localhost' );

我需要从“clientserver”输入任何内容才能输出到“outputserver”流。将有50-60个客户端连接到“clientserver”

1 个答案:

答案 0 :(得分:5)

这应该有效:

var util = require('util'), net = require('net');
var outServer = net.createServer(function(outStream) {
  outStream.on('data', function(data) {
    util.puts(data);
  });
  var inServer = net.createServer(function(inStream) {
    inStream.pipe(outStream, {end: false});
  });
  inServer.listen(7001, 'localhost');
});
outServer.listen(7000, 'localhost');