[erlang-questions] parallel tcp server closed once spawned

YC yinso.chen@REDACTED
Tue Oct 16 03:24:43 CEST 2007


Hi -

I'm going through the exercises in Programming Erlang, and I run into a
puzzling error with the parallel_server example.  Below is my almost
verbatim copy from the book.

para_server() ->
    {ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4},
                     {reuseaddr, true},
                     {active, true}]),
    spawn(fun () ->
          par_connect(Listen) end).

par_connect(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun () ->
          par_connect(Listen) end),
    loop(Socket).

When I try to run the code, I ended up with a badmatch error.

(emacs@REDACTED)12> P5 = spawn(nano_server, para_server, []).
<0.67.0>

=ERROR REPORT==== 15-Oct-2007::18:04:30 ===
Error in process <0.68.0> on node 'emacs@REDACTED' with exit value:
{{badmatch,{error,closed}},[{nano_server,par_connect,1}]}

So gen_tcp:accept(Listen) returns {error, closed} instead of {ok, Socket},
but I'm not sure why, as the gen_tcp:listen line appears to run correctly
(and I've also verified that the seq_server version works).  I must be
missing something extremely obvious here.

Any thoughts are appreciated, thanks.
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071015/d3fcef04/attachment.htm>


More information about the erlang-questions mailing list