Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing simple authentication on a TCP Server from NodeJS
    primarykey
    data
    text
    <p>I am almost sure I don't understand the basic concepts of a TCP server and I'm not treating the data as streams, and worse than that, I might not be using the events very well.</p> <p>I wanted the tcp server to write to the socket 'username: ' so you can then type in your username (on a tcp client like netcat or telnet), and then the tcp server must treat that next data as a username. So far it does work (although I'm almost sure there are better ways to do this), and next it should write 'password: ' so you could type in your password, and if it's the right one, it should then validate the credentials and do more stuff... But what happens is that when you type in the password, it writes again 'password: ' because the <code>socket.write('password: ');</code> is itself inside another <code>socket.on('data',function(){//etc});</code></p> <p>Here's the code I have so far:</p> <pre><code>var net = require('net'); var server = net.createServer(function (socket) { socket.addListener("connect", function () { socket.write('username: '); socket.on('data',function(data){ var username = data.toString().replace('\n',''); socket.write('password: '); socket.on('data',function(data){ var password = data.toString().replace('\n',''); // verify authentication here // Do more stuff }); }); }); }); server.listen('8000','localhost'); </code></pre> <p>What is the correct way to implement this ? I'm only implementing this because I need to receive uploads (streams) that may not have an ending and don't come all at once. Am I being really stupid by trying to solve this problem with a TCP server ?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload