Skip to content

Instantly share code, notes, and snippets.

@msantos
Created December 25, 2010 12:14
Show Gist options
  • Select an option

  • Save msantos/754846 to your computer and use it in GitHub Desktop.

Select an option

Save msantos/754846 to your computer and use it in GitHub Desktop.
Erlang Unix datagram socket
-module(unix_dgram).
-export([client/1, server/0]).
-define(PF_LOCAL, 1).
-define(SOCK_DGRAM, 2).
-define(UNIX_PATH_MAX, 108).
-define(PATH, <<"/tmp/unix_dgram.sock">>).
server() ->
{ok, Socket} = procket:socket(?PF_LOCAL, ?SOCK_DGRAM, 0),
Sun = <<?PF_LOCAL:16/native, % sun_family
?PATH/binary, % address
0:((?UNIX_PATH_MAX-byte_size(?PATH))*8)
>>,
ok = procket:bind(Socket, Sun),
echo(Socket).
echo(Socket) ->
case procket:recvfrom(Socket, 16#FFFF) of
{error, eagain} ->
timer:sleep(10),
echo(Socket);
{ok, Buf} ->
io:format("~p~n", [Buf]),
echo(Socket)
end.
client(Buf) when is_binary(Buf) ->
{ok, Socket} = procket:socket(?PF_LOCAL, ?SOCK_DGRAM, 0),
Sun = <<?PF_LOCAL:16/native, % sun_family
?PATH/binary, % address
0:((?UNIX_PATH_MAX-byte_size(?PATH))*8)
>>,
procket:sendto(Socket, Buf, 0, Sun).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment