erlang:UNIX 域套接字支持吗?

发布于 2024-08-06 01:08:06 字数 86 浏览 9 评论 0原文

有没有办法直接从 Erlang 访问 UNIX 域套接字(例如 /var/run/dbus/system_bus_socket ),而无需借助第三方驱动程序?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞清仓 2024-08-13 01:08:06

Erlang/OTP 仅附带用于 tcp 和 udp 套接字的驱动程序。所以...

不。

第三方驱动程序

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

No.

Third part drivers

喜爱纠缠 2024-08-13 01:08:06

在 Erlang/OTP 19.0 中,UNIX 套接字现已可用,如自述文件中所述 :

OTP-13572 应用程序:erts、内核

相关 ID: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> 

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> 
剩一世无双 2024-08-13 01:08:06

nanomsg 支持 Unix 域套接字,并且 enm 驱动程序nanomsg 提供 Erlang 语言绑定。

例如,要打开请求/响应协议的响应端并绑定到 Unix 域套接字地址:

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

这里,Rep 是一个 nanomsg 套接字。它支持 sendrecv 以及所有常见的 Erlang {active, true |假 |常规 Erlang TCP/SCTP/UDP 套接字提供的 N} 模式等。有关更多详细信息,请参阅 enm github README

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.

茶色山野 2024-08-13 01:08:06

如果你想接收http响应,你可以使用hackney,因为hackney提供了对UNIX套接字的支持。
hackney:get<<"http+unix://path_to_sock_file.sock">>

You can use hackney if you want to recieve http response as hackney provides support to UNIX socket .
hackney:get<<"http+unix://path_to_ sock_file.sock">>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文