Created
December 25, 2010 12:16
-
-
Save msantos/754848 to your computer and use it in GitHub Desktop.
Erlang Unix datagram socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(unix_dgram1). | |
-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, 0, 110) of | |
{error, eagain} -> | |
timer:sleep(10), | |
echo(Socket); | |
{ok, Buf, Sun} -> | |
io:format("~p~n", [Buf]), | |
ok = procket:sendto(Socket, Buf, 0, Sun), | |
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) | |
>>, | |
Src = <<?PF_LOCAL:16/native, | |
0:8, | |
"1234", | |
0:((?UNIX_PATH_MAX-5)*8) | |
>>, | |
ok = procket:bind(Socket, Src), | |
ok = procket:sendto(Socket, Buf, 0, Sun), | |
response(Socket). | |
response(Socket) -> | |
case procket:recvfrom(Socket, 16#FFFF) of | |
{error, eagain} -> | |
timer:sleep(10), | |
response(Socket); | |
{ok, Buf} -> | |
ok = procket:close(Socket), | |
io:format("~p~n", [Buf]) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment