您的位置:首页 > 编程语言 > PHP开发

erlang otp节点间通信

2014-10-13 12:35 246 查看
1.目标:
开启两个节点使得A节点和B节点可以顺利通信

2.步骤与代码:

1)基本操作参看
erlang节点通信小测试

2)代码:

-module(gentm).
-behaviour(gen_server).
%%-type my_struct_type() :: 13.

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3, start_link/0]).
-record(state, {}).
-compile(export_all).
-spec test(ArgName1::integer()) -> any().

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

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

%%--------------------------------------------------------------------
handle_call(Request, _From, State) ->
Reply = ok,
io:format("get call from ~w, request:~w",[_From,Request]),
{reply, Reply, State}.

%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
io:format("get cast :~w",[_Msg]),
{noreply, State}.

%%--------------------------------------------------------------------
handle_info(Info, State) ->
io:format("get info :~w",[Info]),
{noreply, State}.

%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.

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

%% common end--------------

test(V)->
io:format("G~w",[V]) .

spfun()->
receive
{test, V}-> io:format("get testvalue ~w",[V]),
spfun();
Other -> io:format("get othervalue ~w quit!",[Other])
end.

beginsp()->
Pid = spawn(?MODULE,spfun,[]),
register(?MODULE, Pid),
put(cpid,Pid),
Pid.

getfunpid(Node)->
rpc:call(Node, erlang, get, [cpid]).


3.具体过程如下图:

test1:



test2:



4.总结:

genserver节点间通信注意进程节点的注册和发送方的发送方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: