How can a Node.js Socket Server know which IP or d

2019-07-15 11:25发布

问题:

How can I know which IP/hostname the client connect to in node.js TCP Server?

var server = net.createServer(function (stream) { 
    // The server is behind a firewall,
    // how can I know how the client connect to this server?
    console.log(stream.localAddress);
});
server.listen(21, '0.0.0.0');

Thanks

回答1:

Got the ans: We can use stream.address() to get the port and host the client connect to.