managing the tcp/ip data flow and internal messages

info info@REDACTED
Thu Oct 22 16:52:51 CEST 2009


I'm using gen_server to manage several socket connections.
For each connection a process is running, listen a client, reading with gen_tcp:recv

loop(S)->
    case gen_tcp:recv(S,0)->
    {ok,Data} ->
        %% handle data
        loop(S);
    {error,closed} ->
        exit( normal)
    end.

Now I would like to complicate the situation ...
I would like, from another gen_server, to send data to a specific client !

My first problem is to find the process "attached" to a client. Probably I must associate an ident (the client) to each process.
My second problem is to not disturb the tcp/ip connection. I suppose I must use {active,once} and the instruction "receive".

loop(S)->
    inet:setopts(S,[{active,once}]),
    receive
    {tcp,S,Data} ->
        %%handle data
    {tcp_close,S}->
        %%handle close
    {tcp_error,S,Reason}->
        %%handle reason
    {message,Data}->
        gen_tcp:send(S,Data),
        ...
    end.

Is my approach coherent ? robust ? yes/no why ?

John


More information about the erlang-questions mailing list