sockets - Implementing simple authentication on a TCP Server from NodeJS -
i sure don't understand basic concepts of tcp server , i'm not treating data streams, , worse that, might not using events well.
i wanted tcp server write socket 'username: ' can type in username (on tcp client netcat or telnet), , tcp server must treat next data username. far work (although i'm sure there better ways this), , next should write 'password: ' type in password, , if it's right one, should validate credentials , more stuff... happens when type in password, writes again 'password: ' because socket.write('password: ');
inside socket.on('data',function(){//etc});
here's code have far:
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 // more stuff }); }); }); }); server.listen('8000','localhost');
what correct way implement ? i'm implementing because need receive uploads (streams) may not have ending , don't come @ once. being stupid trying solve problem tcp server ?
thanks in advance.
you can use node ftp server source reference.
main problem in code: stream 'data' event gives incoming data split chunks. example, if client issued 2 write('username: u\n') , write('password: p\n') expect data content 'username: u\npassword: p\n' 25 'data' events 'u', 's', 'e', 'r' etc data buffers. need buffer incoming chunks or use state machine.
Comments
Post a Comment