views:

22

answers:

1

I know I can create a tcp server like that in node.js

var dataServer = net.createServer(function (stream) { });

dataServer.on("listening", function() { // this data server listen to a random port // but how can I get the number of port console.log(dataServer.localPort) }

dataServer.listen(0, '0.0.0.0');

But I don't know how to get the port number and send to another service.

Or I should find a random available port and pass to dataServer.listen?

+1  A: 

You can find the address and port the server is listening on using the address method.

dataServer.address();
// => { address: '0.0.0.0', port: 49717 }
Todd Yandell
Do you know the ans for this too:http://stackoverflow.com/questions/3520475/how-can-a-node-js-socket-server-know-which-ip-or-domain-that-the-client-connect-tThanks
Eric Fong