multiple waiting gen_tcp:accepts

Willem Broekema <>
Wed Jul 18 20:59:32 CEST 2001


Is there a reason why there can not be two waiting gen_tcp:accept()'s at 
the same time? In the code below,  when "start()" is called, the 
*second* spawned do_accept will write "{error, einval}" as soon as it is 
started, while I would expect this to behave as:

- A1 and A2 are put in a queue waiting for incoming connections
- A1 gets the first incoming one
- A2 gets the second

start() ->
    {ok, LSock} = gen_tcp:listen(5568,
         [binary, {packet, 0}, {active, true}, {reuseaddr, true}]),
    A1 = spawn_link(?MODULE, do_accept, [LSock, 1]),
    A2 = spawn_link(?MODULE, do_accept, [LSock, 2]),
    ready.

do_accept(LSock, N) ->
    io:write( gen_tcp:accept(LSock) ).


Another question:

"get_func()" below returns a function. Immediatly calling 
"get_func()(3)" gives a syntax error, while "A = get_func(), A(3)" 
behaves ok. What's the reason for that? I guess it has to do with that 
funs are a late addition to Erlang, and the syntax parser didn't have to 
deal with double brackets - "...(..)(..)" - before that?

triple(X) -> 3*X.

get_func() ->
    fun triple/1.

exec_func() ->
    %% this gives an error:
    % get_func()(3).

    %% while this works:
    A = get_func(),
    A(3).


TIA

- Willem




More information about the erlang-questions mailing list