[erlang-questions] using a socket to send and receive

Essien Essien essiene@REDACTED
Thu Aug 19 17:17:36 CEST 2010


Hi Roberto,

On Thu, Aug 19, 2010 at 2:22 PM, Roberto Ostinelli <roberto@REDACTED> wrote:
> 2010/8/19 Essien Essien <essiene@REDACTED>:
>>
>> My approach is still to use {active, once}, thus recieving _all_
>> available data. When I process though, I only take the lenght of data
>> that I need and leave the Rest in a loop variable. The next time
>> around, I prepend the Rest from the previous run to the new coming
>> data, then recurse. So, something like this:
>>
>> % I'm assuming that the socket is delivering
>> % binary not list data.
>> sock_loop(Sock, Len, Rest0) ->
>>        inet:setopts(Sock, [{active, once}]),
>>        receive
>>                {http, Sock, Data0} ->
>>                        % data received from TCP socket, do something with it
>>                        % first prepend pending data in Rest0 before continuing
>>                        Data1 = list_to_binary([Rest0, Data0]),
>>                        <<Data:Len,Rest1/binary>> = Data1,
>>
>>                         % Now you can do what you want with Data
>>                          worker_proc ! <<Data:Len>>,
>>                           sock_loop(Sock, Len, Rest1);
>>                {send, DataToSend} ->
>>                        % data received from external processes, send
>> it to socket
>>                        gen_tpc:send(Sock, DataToSend)
>>        end.
>
> hi essien,
>
> first of all thank you for your reply.
>
> unfortunately this is not enough: you are assuming that you can match
> <<Data:Len,Rest1/binary>> = Data1, which is not true if Len > what you
> have received. therefore, you would need additional checks.

I hear your concerns. In my case, I got away lucky since parsing the
PDU always meant I had to do these checks anyways.

>
> that apart, i don't want to go this way because i find it
> overcomplicated in respect to using passive mode.

true.

>
> there really are no other alternatives?

Not that I know of unfortunately.

cheers,
Essien

>
> thank you,
>
> r.
>


More information about the erlang-questions mailing list