有没有办法直接从Erlang访问UNIX域套接字(例如/ var/run/dbus/system_bus_socket)而无需诉诸第三方驱动程序?
Erlang/OTP仅附带tcp和udp套接字的驱动程序.所以...
没有.
unixdom_drv在http://jungerl.sourceforge.net/
源树的驱动程序示例中的uds_dist
procket at https://github.com/msantos/procket
在Erlang/OTP 19.0中,UNIX套接字现在可用,如自述文件中所述:
OTP-13572应用程序:erts,kernel
相关标识号:PR-612
*突出*
已经实现了对Unix域套接字的实验支持.如果您想尝试一下,请阅读相关信息.示例:gen_udp:open(0,[{ifaddr,{local,"/ tmp/socket"}}]).文档将在用户对实验API的反馈后编写.
例:
lsock.erl:
-module(lsock). -export([watcher/1, test/0]). watcher(Parent) -> {ok, Sockin} = gen_udp:open(0, [{ifaddr, {local, "/tmp/testsockin"}}]), io:format("watcher ok? ~w ~w~n", [ok, Sockin]), Parent ! start, receive Msg -> io:format("watcher got: ~p ~n", [Msg]) end. test() -> file:delete("/tmp/testsockin"), file:delete("/tmp/testsockout"), _ = spawn(lsock, watcher, [self()]), {ok, Sockout} = gen_udp:open(0, [{ifaddr, {local, "/tmp/testsockout"}}]), io:format("parent ok? ~w ~w~n", [ok, Sockout]), receive start -> gen_udp:send(Sockout, {local, "/tmp/testsockin"}, 0, "hi") end.
以下是其结果:
$ erlc lsock.erl $ erl Erlang/OTP 19 [erts-8.0.1] [source-ca40008] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false] Eshell V8.0.1 (abort with ^G) 1> lsock:test(). <0.58.0> parent ok? ok #Port<0.455> watcher ok? ok #Port<0.456> watcher got: {udp,#Port<0.456>,{local,<<"/tmp/testsockout">>},0,"hi"} ok 2>