Erlang gen_server如何一次接收大消息?

时间:2019-01-12 20:17:35

标签: erlang gen-server

我正在编写使用Erlang gen_server在3547端口上侦听的服务器。要将消息发送到服务器,我需要通过以下命令连接到服务器:

telnet localhost 3547

然后输入message并按Enter。问题是,当消息太大时,它会分成多个具有多个请求的较小消息。 我的服务器如何在一个请求中获得所有消息? 这是我的服务器:

%%%===================================================================
%%% API
%%%===================================================================

start_link(Port) ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).

start_link() ->
    start_link(?DEFAULT_PORT).

stop() ->
    gen_server:cast(?SERVER, stop).



%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

init([Port]) ->
    Pid = spawn(fun() -> server(0) end),
    register(state_server, Pid),
    {ok, LSock} = gen_tcp:listen(Port, [{active, true},
                                        {keepalive, true},
                                        {reuseaddr, true}]),

    spawn(fun() -> parallel_connection(LSock) end),
    {ok, #state{port = Port, lsock = LSock}, 0}.


handle_call(_, _From, State) ->
    {reply, {ok, ok, State}}.

handle_cast(stop, State) ->
    {stop, normal, State}.


handle_info({tcp, Socket, RawData}, State) ->
    gen_tcp:send(Socket, io_lib:fwrite("~p~n", [RawData])),
    {noreply, State};

handle_info({tcp_closed, _Socket}, #state{lsock = LSock} = State) ->
    {ok, _Sock} = gen_tcp:accept(LSock),
    {noreply, State};
    % {stop, normal, State};

handle_info(timeout, #state{lsock = LSock} = State) ->
    % {ok, _Sock} = gen_tcp:accept(LSock),
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

0 个答案:

没有答案