[erlang-questions] UDP concurrent server

Sergej Jurečko sergej.jurecko@REDACTED
Wed Dec 9 13:33:27 CET 2015


On Wed, Dec 9, 2015 at 1:23 PM, Benoit Chesneau <bchesneau@REDACTED> wrote:

>
> Well it would allows you to open different socket for the same ports for
> recv. Normally the threads should compete to get the data according to
>
> https://lwn.net/Articles/542629/
>
> So until a process is on another thread or CPU it should increase the
> concurrency.
>
>
Yes that is what the documentation says, but I don't think the actual
implementation on darwin/linux works this way. This simple test code will
work the same on both platforms and all data is received by the first
process:

-module(udptest).
-export([udp/0]).

udp() ->
    Srvrs = [spawn(fun() -> udpsrv(N) end) || N <- lists:seq(1,10)],
    timer:sleep(100),
    [begin spawn(fun() -> udpclient(N) end) end || N <- lists:seq(1,1000)],
    timer:sleep(1000),
    [S ! die || S <- Srvrs],
    ok.

udpsrv(N) ->
    Opts = [{raw, ?SOL_SOCKET, ?SO_REUSEPORT, <<1:32/native>>},
        {active,true},  inet, binary, {recbuf,1024*1024}],
    {ok,S} = gen_udp:open(23232,Opts),
    io:format("Opened server ~p~n",[N]),
    udpsrv(N,S).
udpsrv(N,S) ->
    receive
        {udp, S, _IP, Port, Msg} ->
            io:format("Received msg=~p, srvid=~p, client_port=~p~n",[Msg,
N, Port]),
            udpsrv(N,S);
        die ->
            ok
    end.

udpclient(N) ->
    {ok,S} = gen_udp:open(0),
    gen_udp:send(S,{127,0,0,1},23232,["sending from ",butil:tolist(N)]).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151209/391e1b6e/attachment.htm>


More information about the erlang-questions mailing list