recv消息并写入文件

时间:2011-08-19 13:59:54

标签: erlang

我在这里制作了一个服务器,它从终端接收消息。我想要做的是当服务器从终端接收消息时,它应该将其写入文件。但我不知道应该怎么做。我想在接收函数中编写文件方法file:openfile:write,但它并没有像我想象的那样成功。

-module(tcp).

-behaviour(gen_server).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, start/0, stop/0, connect/0, send/1, recv/0]).

-export([start_link/0]).

%% =============================================================================
%% EXPORTED GEN_SERVER CALLBACKS
%% =============================================================================

init([]) -> {ok, {}}.

handle_call({send, Packet}, _From, State) ->
    gen_tcp:send(State, Packet),
    io:format("Send Working\n"),
    {reply, ok, State};

handle_call(recv, _From, State) ->
     Message = gen_tcp:recv(State, 0),
    io:format("~w~n", [Message]),
    {reply, Message, State}.

handle_cast(connect, _) ->
    case gen_tcp:listen(6888, [binary]) of
    {ok, LSocket}->
        io:format("~w~n", [LSocket]),
        case gen_tcp:accept(LSocket) of 
        {ok, Socket} ->
            inet:setopts(Socket, [{active, false}]),
            io:format("accepted\n"),
            {noreply, Socket};
        Other ->
            error_logger:error_report(["An error occurred which is",Other,"in line",?LINE,"of module",?MODULE])

        end;
    {error, Reason} ->
        error_logger:error_report("An error occurred", Reason, [?LINE,?MODULE])
    end;

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

handle_info(_Info, State) -> {noreply, State}.

terminate(_Reason, _State) -> ok.

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


%% =============================================================================
%% EXPORTED CONVENIENCE FUNCTIONS TO START AND STOP THE SERVER
%% =============================================================================

start() ->
    case gen_server:start({local, ?MODULE}, ?MODULE, [], []) of
        {ok, Pid} ->
            Pid;
        Reason ->
            error_logger:error_report("An error occurred", Reason, [?LINE,?MODULE])
    end.

stop() ->
    case gen_server:cast(?MODULE, stop) of
        ok ->
            ok;
        _ ->
            {error, stop_error}
    end.


%% =============================================================================
%% PUBLIC API FUNCTIONS
%% =============================================================================

connect() -> gen_server:cast(?MODULE, connect).

send(Packet) -> gen_server:call(?MODULE, {send, Packet}).

recv() -> gen_server:call(?MODULE, recv).

write_file(Filename) ->
    {ok, IoDevice} = file:open(test.txt, [write, append]),
    ok.

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


%% =============================================================================
%% LOCAL FUNCTIONS
%% =============================================================================

我按照你的建议做了,并将其作为handle_call实现,但是当我运行时我得到了一个错误,

  

函数handle_call / 3已定义错误

我完全不明白为什么我会收到此错误,因为我有3个handle_call参数,它应该可以工作。

handle_call(write_file, Filename, S) ->
    {ok, File} = file:open(Filename, [append]),
    ok = file:write(File, S),
    ok = file:close(File).

我的API函数

write_file() -> gen_server:call(?MODULE, write_file).

2 个答案:

答案 0 :(得分:3)

您的句柄调用不起作用,因为您有两个定义。你写了:

 handle_call(...) ->
   do_something.

 handle_call(M, F, S) ->
   do_some_other_thing.

您应该通过更改:

使它们成为相同的功能
   do_something.

成:

   do_something;
handle_call(M, F, S) ->
   do_some_other_thing.

答案 1 :(得分:2)

write_file应该执行以下操作:

write_file(Fname, S) ->
    ok = file:write_file(Fname, S, [append]).

write_file(Fname, S) ->
    {ok, File} = file:open(Fname, [append]),
    ok = file:write(File, S),
    ok = file:close(File).