[erlang-questions] should be a parallel tcp server, but can't connect to more than 1 client

Rapsey rapsey@REDACTED
Mon Jan 21 09:53:40 CET 2008


All this program does is listen on a socket, spawn a new process on every
connection and send a never ending stream of numbers to each client that
connects to it. The problem is that once 1 client is connected, no one else
can connect, even though a new acceptor process has been spawned.


start() ->
    case gen_tcp:listen(6002, [binary, {packet, 0}, {active, true},
{reuseaddr, true}]) of
        {ok, Sock} ->
            spawn(fun() -> accept_conn(Sock) end);
        {error, Reason} -> {error, Reason}
    end.


accept_conn(LSock) ->
    case gen_tcp:accept(LSock) of
        {ok, Sock} ->
            spawn(fun() -> accept_conn(LSock) end),
            handle_conn(Sock);
        _ ->
            true
    end.

% wait for http request from browser
handle_conn(Sock) ->
    receive
        {tcp, RecSock, Data} ->
            send_stream(RecSock, 0);
        {tcp_closed, _} ->
            true
    end.

send_stream(Sock, N) when N == 0 ->
    gen_tcp:send(Sock, "HTTP/1.1 200 OK\r\nContent-type:
text/html\r\n\r\n<html><head></head><body>WOHOO<br>"),
    send_stream(Sock, N + 1);
send_stream(Sock, N) ->
    case gen_tcp:send(Sock, integer_to_list(N)) of
        ok ->
            timer:sleep(1000),
            send_stream(Sock, N + 1);
        Any ->
            true
    end.


thank you,
Sergej
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080121/7f8033ca/attachment.htm>


More information about the erlang-questions mailing list