[erlang-questions] Erlang get_tcp:recv data length

Alexander Kuleshov <>
Thu Feb 24 07:52:31 CET 2011


On Wed, Feb 23, 2011 at 7:29 PM, Andrew Thompson <> wrote:
> On Wed, Feb 23, 2011 at 03:46:38PM +0000, Alexander Kuleshov wrote:
>> >Alexander didn't say how many packets he could receive, the separator
>>
>>
>> I don't know how many packets i must receive. I receive smtp must data message
>> Data
>> bla bla bla
>> .
>> Separator  dot - .\r\n
>>
> Actually, you need to check for \r\n.\r\n, just .\r\n by itself isn't
> guranteed to be the end of data.
>
> I receive the message body in gen_smtp by spawning a new process and
> looping doing a recv until the buffer contains "\r\n.\r\n", then I
> return the resulting binary and any trailing data back to the original
> process. I do it in a subprocess to help the garbage collector be able
> to clean all the mess up when the subprocess exits.
>
> https://github.com/Vagabond/gen_smtp/blob/master/src/gen_smtp_server_session.erl#L701
>
> The recv size adjustment is kinda ugly, but it seems to work pretty
> well.
>
> Also, you can use the SMTP SIZE extension to get a vague idea of how
> much data you're going to get. Just don't trust it, its only a
> guideline.
>
> Andrew
>

First of all thank you for your advices.

>Read in packets one at a time and use the binary module to match/split
>on the end-marker in each new package. If you have accumulated up data
>this way, it should be easy to get a boundary point in the right
>packet and then use the part and all preceeding packets as the
>message.

Yes, i just do it.


More information about the erlang-questions mailing list