client.write writes to the kernel buffer, process next tick delays the
execution to the next event loop run. the event loop is faster than
client.write, so you write more data to the buffer, before it gets flushed.
try this:
var client = net.connect({port: 1337}, function () {
console.log('connected');
var i = 0;
function send() {
if (++i < 100) {
client.write('a', send);
} else {
client.end();
}
}
send();
});
i did not test it yet, just interpreting
this http://nodejs.org/api/net.html#net_socket_write_data_encoding_callback
Am Sonntag, 19. August 2012 21:01:09 UTC+2 schrieb Juan Ignacio Dopazo:
>
> Hi!
>
> I'm getting a weird result when writing to a socket. I wrote a simple
> experiment with a client and a server:
>
> server.js
> var net = require('net');
>
> net.createServer(function (connection) {
> console.log('client connected');
> connection.on('data', function (data) {
> console.log('data: ' + data);
> });
> }).listen(1337);
>
> client.js
> var net = require('net');
>
> var client = net.connect({port: 1337}, function () {
> console.log('connected');
> var i = 0;
> function send() {
> client.write('a');
> if (++i < 100) {
> process.nextTick(send);
> } else {
> client.end();
> }
> }
> send();
> });
>
> I expected the server to show 100 lines of data: a, but I ended up
> getting a smaller number of data: aaaaaaa lines. There's
> socket.setNoDelay() that seems to be what I want, but it doesn't seem to
> have any effect.
>
> What am I missing?
>
> Thanks a lot,
> Juan
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en