I'm just getting used to NodeJS so this is a very basic question, please go gentle! I'm trying to reject a connection if the IP address isn't from our local network, it will detect and run the c.close and the console log, but it will still receive data from telnet. I've tried c.close and c.destroy.
Am I using the object 'c' properly? I know its the connectionListener socket, and c.remoteAddress returns the correct IP address for the connecting host!
(please note '172.246.2' is deliberately wrong to make conditional check fail and close the socket)
var server = net.createServer(function(c) {
if (c.remoteAddress.substr(0, 8) !== "172.246.2") {
c.close;
console.log('Socket destroyed');
}
c.on('data', function(data) {
incomingData(data);
});
c.on('error', function(error) {
console.log('Error: ' + error);
});
});
I appreciate there's a lot of basic questions about socket functionality but I couldn't find anything close enough to the question to find an answer!
Cheers in advance, Martin
Call c.destroy() instead of c.close.
close is not a function you can call but it is an event you can subscribe to.