[erlang-questions] Erlang OTP design principle

Eranga Udesh casper2000a@REDACTED
Mon Jun 11 07:08:20 CEST 2007


Hi,

What's the best way to make do_recv/2 function below, OTP compliant? I.e. it
should receive messages from other processes, handle code update requests,
terminate, etc.

Why gen_tcp, gen_udp, etc., are written as behaviors like gen_server, so
that in a handle_*, it will receive incoming messages, etc?

Thanks,
- Eranga




server() ->
    {ok, LSock} = gen_tcp:listen(5678, [binary, {packet, 0}, 
                                        {active, false}]),
    {ok, Sock} = gen_tcp:accept(LSock),
    {ok, Bin} = do_recv(Sock, []),
    ok = gen_tcp:close(Sock),
    Bin.

do_recv(Sock, Bs) ->
    case gen_tcp:recv(Sock, 0) of
        {ok, B} ->
            do_recv(Sock, [Bs, B]);
        {error, closed} ->
            {ok, list_to_binary(Bs)}
    end.






More information about the erlang-questions mailing list