简体   繁体   中英

Node.js TCP forwarder

I have the following simple NodeJS script and want to modify it slightly....

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' );

I need anything coming in from the "clientserver" to be output to the "outputserver" stream. There will be 50-60 clients connecting to the the "clientserver"

This should work:

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM