[erlang-questions] parallel tcp server closed once spawned

Chandru chandrashekhar.mullaparthi@REDACTED
Tue Oct 16 11:46:10 CEST 2007


On 16/10/2007, YC <yinso.chen@REDACTED> wrote:
> Update -
>
> it turns out that if I call para_server directly instead of using spawn, it
> works.  Why this is the case, I'm not fully certain, but my guess is that if
> I call para_server via spawn, then the first process dies right after it
> spawns the accept processes, and causes the connection to close, and hence
> when par_connect is evaluated it finds the listener closed.
>
> If I miss something, please let me know.  Thanks,
> yc
>

You are right, when you spawn, the process which created the socket
dies and the socket is closed. But when you directly call para_server
from the shell, your shell process owns the socket. And as long as you
don't make any mistakes in the shell when typing, your socket will
remain open! See below. I've included a modified version of the
para_server which will work whether you spawn or call it directly from
the shell.

2> par_server:para_server().
<0.45.0>
3>
3> inet:i().
Port Module    Recv Sent Owner    Local Address Foreign Address State
129  inet_tcp  0    0    <0.38.0> *:dbm         *:*             LISTENING
130  undefined 0    0    <0.45.0>               *:*             ACCEPTING
Port Module Recv Sent Owner Local Address Foreign Address State
ok
4> 1/0.

=ERROR REPORT==== 16-Oct-2007::10:41:32 ===
Error in process <0.38.0> with exit value:
{badarith,[{erl_eval,eval_op,3},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {badarith,[{erl_eval,eval_op,3},
                      {shell,exprs,6},
                      {shell,eval_loop,3}]} **

=ERROR REPORT==== 16-Oct-2007::10:41:32 ===
Error in process <0.45.0> with exit value:
{{badmatch,{error,closed}},[{par_server,par_connect,1}]}

>
>
> > > 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.
> >
> > > 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.

para_server() ->
    spawn(fun() ->
		  para_server_1()
	  end).

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

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

loop(Socket) ->
    ok.

cheers
Chandru



More information about the erlang-questions mailing list