当前位置:编程学习 > 网站相关 >>

关于Erlang Socket的三种消息接收模式

erlang的socket有3种消息接收模式:active、passive和active once,可以在gen_tcp:connect/3或gen_tcp:listen/2里设置{active, true | false |once}来实现,也可以用inet:setopts/2来动态设置。这3种模式的区别是:

1. active(主动消息接收):非阻塞。当数据到达时系统会向控制进程发送{tcp, Socket, Data}消息。控制进程无法控制消息流;

2. passive(被动消息接收):阻塞。控制进程必须主动调用recv()来接收消息。可以控制消息流;

3. active once(混合消息接收):半阻塞。这种模式是主动的但仅针对一个消息,在控制进程收到一个消息后,必须显式调用inet:setopts(Socket, [{active, once}])来重新

激活以接收下一个消息。可以进行流量控制。这种模式相对于被动模式来说,有个优点是可以同时等待多个socket的数据。

 


Erlang官方推荐使用active once模式:

Active, once is the recommended way to implement a server in both UDP and
TCP.

The use of active once is superior to the other alternatives from a
programmatical standpoint, it is clean and robust. I don't recommend the
use of active true or active false just for performance reasons, because it
will cause other problems instead.

We are currently working with improvements regarding active once for TCP
since it obviously seems to be slower than necessary. Most probably it is
the same with UDP and we will look into that as well.

We don't think there need any significant performance difference between
active once and the other alternatives and hope to have a solution
confirming that soon (meaning r15b02 or 03)

Regards Kenneth Erlang/OTP, Ericsson

 

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,