TCP/IP Stream and recv?

Per Hedeland <>
Wed Feb 8 20:11:24 CET 2006

Chris Campbell <> wrote:
>This may be a misunderstanding of tcp but here goes.  The following
>client sends two messages to a client one after the other, then waits
>5 seconds and exits.  The server waits for 1 second after accepting a
>connection, then does 1 call to recv.  It only retrieves the first
>message.  Is this Erlang specific or TCP behaviour?  My understanding
>of tcp is that it's a stream and recv may return both messages
>(ignoring fragmentation for the moment).

Generally correct, but... - what did you think that "{packet, 4}" option

>I have only used tcp with single requests like HTTP so far. I'm now
>toying with something that will send messages back and forth hence why
>it's important to know if recv will pull a single message or it just
>pulls everything to make account for this in the protocol parser.

If you use one of the packet options, it will pull a single message -
that's what they do, implement "packet boundaries" on top of TCP. You
could easily do it yourself in the Erlang code (or any other language),
but it's a bit convenient to have the lower levels do it in some cases
(maybe a bit more efficient too).

If you don't use any of the packet options, it behaves just like TCP -
there are no messages, only a byte stream, and your recv could get
anything from 1 byte out of the first message to both messages complete.

--Per Hedeland

More information about the erlang-questions mailing list