Thanks for the help. I guess I still have some C++ habits to forget :)<br><br><br>Sergej<br><br><br><div class="gmail_quote">On Jan 21, 2008 10:54 AM, Joe Armstrong <<a href="mailto:erlang@gmail.com">erlang@gmail.com</a>
> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">What might have happened here is that the process owning the listening<br>socket has died.
<br>Sockets are owned by the process that creates the socket. if the<br>owning process dies the<br>socket is closed.<br><br>If you evaluate start() in the shell then the owning process is the<br>shell command interpreter,
<br>which usually does not die. If you evaluate start() in some other<br>process then it might have died in which case<br>the listening socket will be closed.<br><br>If you change accept_con to print an error you should see this:
<br><div class="Ih2E3d"><br>accept_conn(LSock) -><br> case gen_tcp:accept(LSock) of<br> {ok, Sock} -><br> spawn(fun() -> accept_conn(LSock) end),<br> handle_conn(Sock);<br></div>
X -><br> io:format("OOps:pn", [X])<br> true<br> end.<br><br>The way I'd write accept_con (the way in the Erlang book) is like this:<br><br>accept_conn(LSock) -><br> {ok, Sock} = case gen_tcp:accept(LSock),
<br><div class="Ih2E3d"> spawn(fun() -> accept_conn(LSock) end),<br></div> handle_conn(Sock).<br><br>The way you write things is to silently swallow up the error return<br>with no warnings, which make things difficult
<br>to debug since you get no indication of error. The Erlang way is to<br>crash hard and early.<br><br>If the process owning the listen socket has died then you need to make<br>sure it lives<br>forever. I'd add a sleep(infinity) to the code, like this:
<br><div class="Ih2E3d"><br>start() -><br> case gen_tcp:listen(6002, [binary, {packet, 0}, {active, true},<br>{reuseaddr, true}]) of<br> {ok, Sock} -><br></div> spawn(fun() -> accept_conn(Sock) end), sleep(infinity);
<br><div class="Ih2E3d"> {error, Reason} -> {error, Reason}<br> end.<br><br></div>where<br><br>sleep(T) -> receive after T -> void end.<br><br><br>This version of start can be happily spawned - in the original version
<br>you must ensure that the process evaluating<br>start() does not die.<br><br>Hope this helps<br><br>/Joe Armstrong<br><br><br><br>2008/1/21 Rapsey <<a href="mailto:rapsey@gmail.com">rapsey@gmail.com</a>>:<br><div>
<div></div><div class="Wj3C7c">> All this program does is listen on a socket, spawn a new process on every<br>> connection and send a never ending stream of numbers to each client that<br>> connects to it. The problem is that once 1 client is connected, no one else
<br>> can connect, even though a new acceptor process has been spawned.<br>><br>><br>> start() -><br>> case gen_tcp:listen(6002, [binary, {packet, 0}, {active, true},<br>> {reuseaddr, true}]) of<br>
> {ok, Sock} -><br>> spawn(fun() -> accept_conn(Sock) end);<br>> {error, Reason} -> {error, Reason}<br>> end.<br>><br>><br>> accept_conn(LSock) -><br>> case gen_tcp:accept(LSock) of
<br>> {ok, Sock} -><br>> spawn(fun() -> accept_conn(LSock) end),<br>> handle_conn(Sock);<br>> _ -><br>> true<br>> end.<br>><br>> % wait for http request from browser
<br>> handle_conn(Sock) -><br>> receive<br>> {tcp, RecSock, Data} -><br>> send_stream(RecSock, 0);<br>> {tcp_closed, _} -><br>> true<br>> end.<br>
><br>> send_stream(Sock, N) when N == 0 -><br>> gen_tcp:send(Sock, "HTTP/1.1 200 OK\r\nContent-type:<br>> text/html\r\n\r\n<html><head></head><body>WOHOO<br>"),<br>
> send_stream(Sock, N + 1);<br>> send_stream(Sock, N) -><br>> case gen_tcp:send(Sock, integer_to_list(N)) of<br>> ok -><br>> timer:sleep(1000),<br>> send_stream(Sock, N + 1);
<br>> Any -><br>> true<br>> end.<br>><br>><br>> thank you,<br>> Sergej<br>><br></div></div><div><div></div><div class="Wj3C7c">> _______________________________________________
<br>> erlang-questions mailing list<br>> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>> <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions
</a><br>><br></div></div></blockquote></div><br>