run a tcp server to listen to a port range

Ryan Schmidt ryandesign at macports.org
Tue Mar 13 22:53:18 PDT 2012


On Mar 13, 2012, at 21:42, michael sparacio wrote:

> I am not familiar with js but trying to get an example.js coded properly, this is only listening on the final port, the 10100...
> 
> var net = require('net');
> var port = 10000
> for (port = 10000; port < 10100; port++) {
>  ;
> }
> var server = net.createServer(function(c) { //'connection' listener
>  console.log('server connected');
>  c.on('end', function() {
>    console.log('server disconnected');
>  });
>  c.write('hello\r\n');
>  c.pipe(c);
> });
> server.listen(port, function() { //'listening' listener
>  console.log('server bound');
> });
> 
> I assume I am making a dumb error??

Your program initializes a variable "port" to the value 10000, then increments it until it reaches 10100. It then creates a server and assigns it to the variable "server", then tells the server to listen on that port. If you want to create multiple servers that each listen on a different port, you'll want to create the servers inside the loop.

But we are veering off-topic here. The MacPorts mailing lists are not well suited to discussing JavaScript programming. If you have further questions that aren't answered by reading the nodejs documentation and sample programs, you should ask on the nodejs mailing list:

http://groups.google.com/group/nodejs




More information about the macports-users mailing list