{ok,Socket} = gen_udp:open(9000, [binary,{active,false}, {reuseaddr,true}]),
Pid0 = spawn_link( fun() -> loop_passive( Socket ) end ),
Pid1 = spawn_link( fun() -> loop_passive( Socket ) end ) --> error
.. (many overlapped receiving per a binded socket)
N
loop_passive(Socket) ->
case gen_udp:recv(Socket, 0) of
{ok, {Addr,Port,Packet}} ->
process(Socket,Addr,Port,Packet),
loop_passive(Socket);
Error ->
io:format("udp_passive error: ~p~n", [Error])
end.
I’ll make scalabe UDP server.
Erlang UDP mechanism was simple once at a time.
Ever, there is not another way?
Let us look at that code where it is readable:
This means that
self()opens the UDP socket, andPid0andPid1gets passed the resulting socket. Note thatself()(the process running the above code) is the controlling process of the socket.This is just a simple loop on incoming packets. It means that
Pid0andPid1are the ones that are reading off data from the socket, whenever goes first.The usual problems in this case are: