erlang的Socket参数含义

{ok, Listen} = gen_tcp:listen(?defPort, [binary, {packet, 2},{reuseaddr, true},{active, true}]),
%gen_tcp表用TCP连接
%binary表二进制流方式
%packet,2:表包头长度2字节
%reuseaddr, true:表多个实例可重用同一端口
% {active,true} 创建一个主动套字节(非阻塞)
% {active,false} 创建一个被动套字节(阻塞),如果为false表必须手工处理阻塞,否则阻塞在此处无法收听,当前我无法处理
%{active, once} 创建一个一次性被动套字节(阻塞),只收听一次后堵塞,必须调用inet:setopts(Socket, [{active, once}]),后才可收听下一条
% {active,once} 创建一个主动套字节仅接收一条消息,如想接收下一条必须再次激活(半阻塞)

seq_loop(Listen).


listen_socket(Socket,Mode)->
receive 
{tcp,Socket,Bin} ->
%process_req(Bin),
%MsgQueueSize->获得有多少个消息包积压,先使用{active, true},如果判断到接收到的消息包太多,再改成 {active, once}。
{message_queue_len, MsgQueueSize} = erlang:process_info(self(),message_queue_len),
io:format("Server received binary = ~p~n",[Bin]),
io:format("Queue size is ~p~n", [MsgQueueSize]),
if
(MsgQueueSize > 500) and (Mode =:= active_true) ->
inet:setopts(Socket, [{active, once}]), %再次调用收听,开始收听下一条信息 
listen_socket(Socket, active_once);
(MsgQueueSize < 10) and (Mode =:= active_once) ->
inet:setopts(Socket, [{active, true}]),   
listen_socket(Socket, active_true)
true->
io:format("Queue size is ~p~n", [MsgQueueSize]),   
listen_socket(Socket, Mode)
end;
{tcp_closed,Socket} ->
io:format("有人下线 =>~n"),
io:format("Client socket closed ~n")
end.

你可能感兴趣的:(erlang的Socket参数含义)