[erlang-questions] UDP concurrent server

Bengt Johansson E bengt.e.johansson@REDACTED
Wed Dec 9 11:35:51 CET 2015

Hi Bogdan!

Your questions make me a bit confused. I wonder *why* you want two processes waiting for packets from the same socket and *what* you expect to happen?

Generally one usually only speaks about concurrent servers regarding TCP where you bind to a socket waiting for incoming connections and once a connection is established, you spawn a process (or thread depending on language) and process the data coming over the connection concurrently.
Note that the main loop of the server that waits for responses is always sequential. The new process handling the connection gets a new socket with a free port used only for that particular connection.

But! UDP lacks support for connections mainly since it is a message based protocol and hence is devoid of any connection abstraction ☺

Still the question remains what is a concurrent UDP server? I guess what you want to achieve is some kind of distribution of incoming packets to several processes to handle them. In that case you should either go for the solution of TCP to set up new socket for each communication or write a simple process that classifies the incoming packets and distributes them to the correct process based on some information in the packet, fi. Some identifier you have chosen to identify the connection. Basically  you have to implement an application specific load balancer – or rather load distributor – hoping that the time taken to actually process the packets greatly overshadows the time it takes to distribute them. ☺

Anyway, there is no way the underlying UDP stack and/or erts can make the decision for you. To which process to send the packet that is.

Hope that helps!
BR, BengtJ

From: erlang-questions-bounces@REDACTED [mailto:erlang-questions-bounces@REDACTED] On Behalf Of Bogdan Andu
Sent: den 9 december 2015 10:15
To: Erlang
Subject: [erlang-questions] UDP concurrent server

following the thread https://groups.google.com/forum/?hl=en#!topic/erlang-programming/6Q3cLtJdwIU
as it seems that POSt to topic does not work

After more tests the basic questions that remains ..

Is there a way to have more than one process be blocked
in gen_udp:recv/2 call as this seems to not be possible,
probably because the way udp sockets work.

Sequentially works as expected, but when when I try to spawn another process
that makes and attempt to execute gen_udp:recv/2 while the first process already does
gen_udp:recv/2 , the second process gives elready error. This means that 2 process
cannot concurrently do gen_udp:recv/2 .

In scenario with socket {active, once} or {active, true} there is only one process that can
receive the message from socket (the one that does gen_udp:open/2 ),
and for multi-threaded applications this quickly can become a bottleneck.
In this case, however, elready error disappears of course.
I tried both variants and both have disavantages.

Is there an idiom for designing a udp concurrent server in Erlang?
So far, it seems impossible.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151209/645d6e7b/attachment.htm>

More information about the erlang-questions mailing list