[erlang-questions] process: messages from various sources

Bengt Kleberg <>
Fri Apr 3 08:05:10 CEST 2009


Greetings,

Just to make sure: The socket returned from listen/2 can only be used in
calls to accept/1,2. The socket you get from accept() is the one you
should use in my_process/1 below.

Also, please consider adding 
{tcp_closed, Socket}
and
{tcp_error, Socket, Reason}
to your receive.


bengt

On Thu, 2009-04-02 at 18:15 +0200, Gamoto wrote:
> OK, like this ?:
> {ok, Listen} = gen_tcp:listen(Port,[binary, {packet, 0}, {active, once}, {reuseaddr, true}] ),
> ...
> 
> 
> my_process(Socket)->
> 	receive
> 		{tcp,Socket,Bin}->  handle_Bin,
> 				    inet:setopts(Socket, [{active, once}]),
> 			            my_process(Socket);     
> 
>         {msg,Message} -> handle Message,
> 			 my_process(Socket);  
>     end.
> 
> 
> >Sure, however, you need to make a call to inet:setops/2 only if you receive
> >a message from socket (hence, do not need to do it if you receive {msg,
> >Message}.
> >The second observation is matter of style -- I personally wouldn't use macro
> >to declare options, as it hides what is actually used, and you would need to
> >browse the code, or even go to another file yo find out what's going on.
> >
> >-----Original Message-----
> >From: Gamoto [mailto:] 
> >Sent: 02 April 2009 03:29 PM
> >To: Valentin Micic
> >Subject: Re: RE: [erlang-questions] process: messages from various sources
> >
> >Thank you for your explanations. Does this code better and correct ?    the
> >gen_tcp:recv function is unuseful in my case, isn't it ?
> >
> >-define(TCP_OPTIONS,[binary, {packet, 0}, {active, once}, {reuseaddr,
> >true}]).
> >
> >{ok, Listen} = gen_tcp:listen(Port, ?TCP_OPTIONS),
> >...
> >
> >
> >my_process(Socket)->
> >	receive
> >		{tcp,Socket,Bin}->  handle_Bin,
> >				    inet:setopts(Socket, [{active, once}]),
> >			            my_process(Socket);     
> >
> >        {msg,Message} -> handle_Message,
> >			 inet:setopts(Socket, [{active, once}]),
> >                         my_process(Socket);  
> >    end.
> >
> >
> >>Absolutely!
> >>However, you must ensure that socket is in a right mode to achieve that. In
> >>other words, you can use gen_tcp:recv, or receive it as a message depending
> >>on how "active" parameter is set during the socket creation (or using
> >>inet:setopts/2 function. Thus:
> >>
> >>{active, false} indicate that socket will be used using gen_tcp:recv/2
> >>function
> >>
> >>{active, true} indicates that socket will send all messages to process'
> >>message queue (where it may be retrieved using receive...end construct.
> >>
> >>{active, false} indicates that socket will send only one message to message
> >>queue, and the rest shall be kept in socket's (kernel) buffer. In order to
> >>trigger sending of the next message, inet:setopts/2 with another {active,
> >>onece}.
> >>
> >>BR.
> >>
> >>V.
> >>
> >>-----Original Message-----
> >>From: 
> >>[mailto:] On Behalf Of Gamoto
> >>Sent: 02 April 2009 12:11 PM
> >>To: erlang-questions
> >>Subject: [erlang-questions] process: messages from various sources
> >>
> >>Is this possible for one process to receive messages from two sources whose
> >>one of these is a socket ?
> >>
> >>Example
> >>
> >>my_process(Socket)->
> >>	receive
> >>		{tcp,Socket,Bin}->                %equivalent to
> >>gen_tcp:recv(Socket,0) ????
> >>			...                                   %handle Bin
> >>			my_process(Socket);       %loop
> >>        {msg,Message} ->                %from another process
> >>		    ...                                   %handle Message
> >>i.e gen_send(Socket,Message)
> >>            my_process(Socket);       %loop
> >>    end.
> >>
> >>_______________________________________________
> >>erlang-questions mailing list
> >>
> >>http://www.erlang.org/mailman/listinfo/erlang-questions
> >>
> >
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list