[erlang-questions] how to limit number of connections to gen_tcp?

Alin Popa alin.popa@REDACTED
Tue May 26 19:47:00 CEST 2009


Hi Jarrod,

Something like this might help you:
*
-define(MAX_TCP_CONNECTIONS, 1000).
-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr,
true}, {backlog, ?MAX_TCP_CONNECTIONS}]).

listen(Port, F) ->
     {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
     accept(LSocket, F).

.............*

Hope this is what you are looking for ...

On Tue, May 26, 2009 at 8:09 PM, Jarrod Roberson <jarrod@REDACTED>wrote:

> I have the following code. How do I limit the number of connected clients.
>
> -module(linereceiver).
>
> -export([start/0]).
>
> sleep(T) ->
>    receive
>       after T ->
>           true
>    end.
>
> start() ->
>    spawn(fun() ->
>            start_parallel_server(3000),
>            sleep(infinity)
>          end).
>
> start_parallel_server(Port) ->
>    {ok, Listen} = gen_tcp:listen(Port, [binary,
> {packet,line},{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),
>    inet:setopts(Socket, [{packet, line}, list, {nodelay, true},
> {active, true}]),
>    io:format("Connection Made!~n"),
>    get_line(Socket).
>
> get_line(Socket) ->
>    receive
>        {tcp, Socket, Line} ->
>            io:format("Received Line:~p~n", [Line]),
>            get_line(Socket);
>        {tcp_closed, Socket} ->
>            io:format("Connection Closed!~n"),
>            void
>    end.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
Regards,

Alin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090526/b69960e0/attachment.htm>


More information about the erlang-questions mailing list