title: How to secure tcp connection - nodejs?
body: <p>I have following <code>sample code</code> used in the <code>tcp server</code> </p>
  
  <pre><code>var server = net.createServer();
  
  server.on('connection', function (socket) {
  
     if(restrictedIP == sock.remoteAddress){
        //How to close the particular connection safely without 
        //affecting other connections
     }
  
    socket.on('data', function(data) {
      console.log(data);        
    });
  
    socket.on('close', function(data) {
      console.log('client disconnected');
    });
  });
  
  server.listen(3000, '127.0.0.1');
  </code></pre>
  
  <h3>Note : I have a check to authenticate the client/server ip (3rd line).</h3>
  
  <h3>Problem :</h3>
  
  <ol>
  <li><p>Whether this logic sounds good to authenticate the client/server.</p></li>
  <li><p>Main thing, How to close the particular connection from the restricted ip address.(4th line - comment)</p></li>
  </ol>
  
  <h3>Scenario :</h3>
  
  <p>I have two servers one is client server which is express/http server lets name it as <code>server1-express</code>, and other one is tcp server named as <code>server2-tcp</code>. </p>
  
  <ol>
  <li><p><code>server1-express</code> server will talk to <code>server2-tcp</code> using tcp.</p></li>
  <li><p><code>server2-tcp</code> should only allow this particular <code>server1-express</code> server, It shouldn't allow connection to any other ip, </p></li>
  <li><p>Using socket.destroy() will crash the <code>server2-tcp</code>.</p></li>
  <li><p>Using socket.end() will force us to <code>write</code> to servers.</p></li>
  </ol>
  
  <h3>How to allow only particular <code>ip</code> and deny all other <code>ip</code> access?</h3>
  
  <p>Any help or suggestion will be grateful</p>