On Fri, Sep 7, 2012 at 7:05 PM, Mark Volkmann <r.mark.volkm...@gmail.com>wrote:

> I don't doubt you. I just want to see that documented somewhere or clearly
> see how that is enforced in the code. I have looked at net.js. It's not the
> easiest code to follow. If it's clear from that code, I could use some
> pointers on where to look.
>
> I'll try to make my concern more clear below with very simple code for
> server.js and client.js that really runs. Run "node server" in one window
> and "node client" in another.
> The numbered comments indicate the order in which I expect the lines to
> execute.
> It seems clear to me that 4 could run before 5. If that happens, how is it
> that the server still gets that message? Is the message being buffered
> until connListener completes?
>
>
4 always runs before 5. The data event cannot be triggered unless something
is written to the socket. This is provided the server.js file is run first
and then client.js. The other way round will not work as it will throw a
ECONNREFUSED on connect() at line 3 (provided the server has not done a
listen() yet).

Here is the flow:
Behind the scenes, the event loop is notified of a new connection. It
accept()'s the connection. Once accept()ed the connListener callback is
triggered and the particular socket (wrapper to the raw socket) is passed
as an argument. Listeners for various events (such as "data", "end",
"chunk" etc) are setup. Then, a read event handler is registered on the raw
socket's FD (UV_READ). When the file descriptor becomes readable, the data
is recv()d and is stored in a buffer and "chunk" event is emitted for each
data received (recv() returns the size of the data and "errno" is EAGAIN
until all data is read into the buffer). When the buffer is full (recv()
returns a -1), the "data" event is emitted with the buffer as argument. If
recv() returns 0, the socket is deemed to have closed on the other end. The
read event handler is then stopped, a full shutdown of the raw socket is
performed and the file descriptor is detached from the event loop with the
"end" event being emitted.  (i have simplified it a lot. this is how its
generally done).


> server.js
>
> var net = require('net');
> function dataListener(data) {
>   console.log('received', data.toString());
> }
> function connListener(socket) {
>   socket.on('data', dataListener); // 5
> }
> var server = net.createServer(connListener); // 1
> server.listen(8019); // 2
>
> client.js
>
> var net = require('net');
> var socket = net.connect(8019); // 3
> socket.write('Is this lost?'); // 4
>
> On Fri, Sep 7, 2012 at 7:29 AM, ribao wei <riba...@gmail.com> wrote:
>
>> It is not about node.js, it is a Javascript thing.
>>
>> Socket will not receive any data util it "connect".
>>
>>
>> On Thu, Sep 6, 2012 at 11:35 PM, Hsu Ping Feng <fillano.f...@gmail.com>wrote:
>>
>>> https://github.com/joyent/node/blob/master/lib/net.js
>>>
>>> It is a good way to understand through source code.
>>>
>>>
>>> 2012/9/7 Mark Volkmann <r.mark.volkm...@gmail.com>
>>>
>>>> On Thu, Sep 6, 2012 at 4:40 PM, Jorge <jo...@jorgechamorro.com> wrote:
>>>>
>>>>> On 06/09/2012, at 23:11, Mark Volkmann wrote:
>>>>>
>>>>
>>>>
>>>>> My understanding is that node won't/shouldn't emit/dispatch any 'data'
>>>>> events to the socket 'socket' until *after* having called cb(socket), 'cb'
>>>>> being the callback function passed on to .createServer(cb).
>>>>>
>>>>
>>>> I suspect you are correct. I'd like to understand how Node implements
>>>> that. It would be great if this is already documented somewhere and I could
>>>> just read that to understand it.
>>>>
>>>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>
> --
> 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to