Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the IP Address of the connecting client in Node.js?

Tags:

node.js

I have two questions:

1) How can I get the IP address, and any other possible data about the client when it connects (see comment in code in the connect section)

2) Will this code safely allow multiple client connections at the same time?

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

var server = net.createServer(function (stream) {
  stream.setEncoding('utf8');

  stream.on('connect', function() {
    ///////////////////////////////////////////////////////
    console.log("WANT THE IP OF THE CONNECTOR HERE!!!!!!");
    ///////////////////////////////////////////////////////
  });

  // data recieve
  stream.on('data', function (data) {
    //stream.write(data);
    console.log("recv: [" + data + "]");
  });

  // end connection
  stream.on('end', function () {
    stream.end();
  });

});
server.listen(50505, 'localhost');
like image 991
MichaelICE Avatar asked Aug 11 '11 03:08

MichaelICE


1 Answers

  1. http://nodejs.org/docs/v0.3.2/api/net.html#stream.remoteAddress

  2. Yes

like image 171
Dan Grossman Avatar answered Sep 18 '22 13:09

Dan Grossman