Socket module (NIF) UDP example?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Socket module (NIF) UDP example?

pablo platt-3
Hi,

I have a gen_udp server in production and I want to test the performance of the new socket NIF.

How can I receive a message asynchronously similar to {active, once} in gen_udp?
Do I need to keep a queue of outgoing packets in case the send buffer is full and resend them? Can I just increase the send buffer and ignore this complexity?

With gen_udp I open the socket with random port:
{ok, Socket} = gen_udp:open(0, [binary, {active, once}, {recbuf, 4096}])

Send data to a remote socket:
gen_udp:send(Socket, Addr, Port, Data),

Receive data with {active, one}
handle_info({udp, Socket, Addr, Port, Data}, State) ->
  % process data
  inet:setopts(Socket, [{active, once}]),
  {noreply, State}.

Close the socket:
gen_udp:close(Socket),