[erlang-questions] UDP client/server performance
Michael Santos
michael.santos@REDACTED
Tue Aug 14 11:40:19 CEST 2012
On Tue, Aug 14, 2012 at 08:25:23AM +0200, Ronny Meeus wrote:
> Is there any code available that implements a NIF to have access to UDP sockets?
> I have found the procket implementation but it looks like it is using
> a separate process to communicate with the socket. This sounds like
> overkill to me for this test.
You can get the socket directly using procket:socket/3. See
procket:sendto/4 and procket:recvfrom/4 as well for retrieving the
struct sockaddr for the peer.
-module(udp_srv).
-export([s/0]).
-define(UINT16(N), N:2/native-unsigned-integer-unit:8).
-define(PF_INET, 2).
-define(PORT, 6001).
s() ->
{ok,FD} = procket:socket(inet, dgram, 0),
% sockaddr for Linux
Sockaddr = <<?UINT16(?PF_INET), ?PORT:16, 0:32, 0:64>>,
ok = procket:bind(FD, Sockaddr),
Ref = erlang:open_port({fd, FD, FD}, [stream, binary]),
loop(Ref).
loop(Ref) ->
receive
{Ref,{data,Data}} ->
error_logger:info_report(Data),
loop(Ref)
end.
More information about the erlang-questions
mailing list