开发者

Accessing socket from two processes

开发者 https://www.devze.com 2023-01-14 15:47 出处:网络
Is it开发者_如何转开发 allowed to have one process reading from and another process writing to a socket in Erlang?

Is it开发者_如何转开发 allowed to have one process reading from and another process writing to a socket in Erlang? I have tried it and it appears to work but I would like to know if it's foolproof.


As I understand it from the source code, a (gen_tcp, at least) socket send/recv boils down to an erlang:port_command for the send and an erlang:port_control for the recv on the socket port (see prim_inet.erl).

For port_command: "if the port is busy, the calling process will be suspended until the port is not busy anymore." The port_control is also a synchronous operation.

Correct me if I'm wrong, but it would appear to be completely safe to use multiple processes to read and write to a socket.


I am pretty sure I remember doing this without any problems.


Anyone of them can write, but it wouldn't make sense if all your processes could receive. It would overflow the mailboxes of all the ones not anticipating messages. You need to define only one Pid to receive packets, by default it's whatever Pid the socket was created in. You can set any Pid to control the socket by setting the controlling process.

0

精彩评论

暂无评论...
验证码 换一张
取 消