Interrupting passive socket recv

Luke Gorrie <>
Mon Nov 13 18:03:05 CET 2000

Sean Hinde <> writes:

> Trying to be as precise as possible about an idea, here goes:
> I'm thinking about an application in which my Erlang node might get flooded
> with replies to a query it has sent over a socket.
> This situation would appear to lend itself to a passive gen_tcp:recv/2 call
> rather than an active socket so that the message buffer doesn't chew up all
> the memory of my machine while it tries to deal with the flood.

I think the {active, once} feature would be good for this, but at a
glance it appears to be undocumented (?) but present in R7B (and older
versions I think).

My understanding is that you make a socket {active, once}, either as
an option in connect/3 or later with setopts/2, it will send you one
packet "actively" and then immediately switch to passive mode. When
you receive the packet in your erlang code, you can set the socket
back to {active, once} again for the next packet. I guess this is what
you need.

Example usage connecting to the "echo" daemon:

| 5> {ok, S} = gen_tcp:connect("localhost", 7, [{active, once}]). 
| {ok,#Port<0.7>}
| 6> gen_tcp:send(S, "foo").
| ok
| 7> flush().
| Shell got {tcp,#Port<0.7>,"foo"}
| ok
| 8> gen_tcp:send(S, "bar").
| ok
| 9> flush().
| ok
| 10> inet:setopts(S, [{active, once}]).
| ok
| 11> flush().                          
| Shell got {tcp,#Port<0.7>,"bar"}
| ok


More information about the erlang-questions mailing list