erlang: UNIX domain socket support?

青春壹個敷衍的年華 提交于 2019-11-29 13:05:26

问题


Is there a way to access UNIX domain sockets (e.g. /var/run/dbus/system_bus_socket ) directly from Erlang without resorting to a third-party driver?


回答1:


Erlang/OTP comes with drivers for tcp and udp sockets only. So...

No.

Third part drivers

  • unixdom_drv in http://jungerl.sourceforge.net/
  • uds_dist in the source tree's driver examples
  • procket at https://github.com/msantos/procket



回答2:


In Erlang/OTP 19.0, UNIX Sockets are now available, as stated in the readme:

OTP-13572 Application(s): erts, kernel

Related Id(s): PR-612

* HIGHLIGHT *

Experimental support for Unix Domain Sockets has been implemented. Read the sources if you want to try it out. Example: gen_udp:open(0, [{ifaddr,{local,"/tmp/socket"}}]). Documentation will be written after user feedback on the experimental API.

Example:

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.

And the following demonstrates its results:

$ 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> 



回答3:


The nanomsg library supports Unix domain sockets, and the enm driver provides an Erlang language binding for nanomsg.

For example, to open the response side of a request/response protocol and bind to a Unix domain socket address:

Url = "ipc:///path/to/socket/file",
{ok,Rep} = enm:rep([{bind,Url}]),

Here, Rep is a nanomsg socket. It supports send and recv as well as all the usual Erlang {active, true | false | N} modes, etc. that regular Erlang TCP/SCTP/UDP sockets provide. For more details consult the enm github README.



来源:https://stackoverflow.com/questions/1478831/erlang-unix-domain-socket-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!