Erlang 接收消息并写入文件
我在这里做了一个服务器,它接收来自终端的消息。我想做的是,当服务器从终端接收到消息时,它应该将其写入一个文件。但是我不知道我该怎么做。我曾想过在receive函数中编写file方法Erlang 接收消息并写入文件,erlang,Erlang,我在这里做了一个服务器,它接收来自终端的消息。我想做的是,当服务器从终端接收到消息时,它应该将其写入一个文件。但是我不知道我该怎么做。我曾想过在receive函数中编写file方法file:open,file:write,但结果并不像我想象的那样 -module(tcp). -behaviour(gen_server). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change
file:open
,file: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\u调用
,但当我运行时,我得到一个错误
函数句柄\u调用/3已定义错误
我不明白为什么会出现这个错误,因为我有3个参数用于handle_调用,它应该可以工作
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).
write_文件
应执行以下操作:
write_file(Fname, S) ->
ok = file:write_file(Fname, S, [append]).
或
write_文件
应执行以下操作:
write_file(Fname, S) ->
ok = file:write_file(Fname, S, [append]).
或
句柄调用不起作用,因为它有两个定义。你写了:
handle_call(...) ->
do_something.
handle_call(M, F, S) ->
do_some_other_thing.
您应通过更改以下内容使其成为相同的函数:
do_something.
进入:
句柄调用不起作用,因为它有两个定义。你写了:
handle_call(...) ->
do_something.
handle_call(M, F, S) ->
do_some_other_thing.
您应通过更改以下内容使其成为相同的函数:
do_something.
进入:
好了,那么,请不要说“你好”和“谢谢”,而要关心排版。它增加了获得好答案的可能性,也有助于你的问题的未来读者。好了,请不要说“你好”和“谢谢”,而要注意排版。它增加了获得好答案的可能性,并有助于你问题的未来读者。