node.js - poblem in receiving data using net.Server -
i have problem in receiving data, when tried telnet server every time type in telnet, sever log each character typed,so how data ? or complete data stream ?
var svr = require('net'); svr.creteserever(function(socket){ console.log("client connected"); socket.on('data',function(data){ console.log("the socket",data.tostring()); }); }); svr.listen(3030, 'localhost', false, function () { console.log('server listening in port 3030'); });
thank in advance.
you can collect data until have enough, or until remote side done:
var chunks = []; socket.on('data', function(data) { chunks.push(data); // here can check if have enough }).on('end', function() { var = buffer.concat(chunks); // here have data client sent });
edit: judging comments you're not interested in reading all data, in reading line-by-line. in case, can use readline
module:
var net = require('net'); var readline = require('readline'); net.createserver(function(socket) { var rl = readline.createinterface({ input : socket }); rl.on('line', function(l) { console.log('line:', l); }); }).listen(...);
Comments
Post a Comment