0

I'm trying to make a server based on the net module. what I don't understand is on which event I'm supposed to put the response code: on(data,function()) could still be in the middle of receiving more data from the stream (so it might be to early to reply) and on(end,function()) is after the connection is closed .

thank you for your help

3
  • Do you want to do raw TCP or HTTP? Commented Dec 4, 2011 at 18:22
  • TCP has no concept of a message, it models just a stream of bytes. You'll have to listen to data and detect your own message format in the stream. Commented Dec 4, 2011 at 18:27
  • But how can I detect it? for example, how is it detected for the http protocol? Commented Dec 4, 2011 at 18:34

1 Answer 1

2

The socket event ('data'), calls the callback function every time an incoming data buffer is ready for reading,, and the event emits the socket buffer of data,, so use this,,

socket.on('data',function(data){
    // Here is the function to detect the real data in stream
});

this can help for node v0.6.5, http://nodejs.org/docs/v0.6.5/api/net.html#event_data_

and this for clear understanding for the Readable streames, http://nodejs.org/docs/v0.6.5/api/streams.html#readable_Stream

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but I still don't understand what will be the right way to detect the end of my message format
what's your message formate ?,, inside the callback function place the code require to detect the end of it,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.