• erlang节点局域网通信


    节点1:

    F:WorkSpaceServersrc>erl -name hw@192.168.10.142 -setcookie 4213
    consulting .erlang in "F:/WorkSpace/Server/src"
    Now  in:"F:/WorkSpace/Server/src"
    Eshell V5.9.3.1  (abort with ^G)
    (hw@192.168.10.142)1> auth:get_cookie().
    '4213'
    (hw@192.168.10.142)2> hw:start().
    true
    (hw@192.168.10.142)3>

    步骤
    启动节点: erl -name hw@192.168.10.142 -setcookie 4213

    启动进程: hw:start().

    注意: 本地机器不同节点可以用都短名 -sname, 跨网络涉及到DNS要用-name

    节点2:

    F:WorkSpaceServersrc>erl -name bibo@192.168.10.142 -setcookie 4213
    consulting .erlang in "F:/WorkSpace/Server/src"
    Now  in:"F:/WorkSpace/Server/src"
    Eshell V5.9.3.1  (abort with ^G)
    (bibo@192.168.10.142)1>
    net_adm:ping(hw@192.168.10.142).
    * 2: syntax error before: 168.1
    (bibo@192.168.10.142)1> net_adm:ping(hw@192.168.10.142).
    * 1: syntax error before: 168.1
    (bibo@192.168.10.142)1> net_adm:ping('hw@192.168.10.142').
    pong
    (bibo@192.168.10.142)2> node().
    'bibo@192.168.10.142'
    (bibo@192.168.10.142)3> auth_getcookie().
    ** exception error: undefined shell command auth_getcookie/0
    (bibo@192.168.10.142)4> auth:get_cookie().
    '4213'
    (bibo@192.168.10.142)5> net_adm:ping('hw@192.168.10.142').
    pong
    (bibo@192.168.10.142)6> node().
    'bibo@192.168.10.142'
    (bibo@192.168.10.142)7> rpc:call(hw@192.168.10.142,hw,store,[weather,cold]).
    * 1: syntax error before: 168.1
    (bibo@192.168.10.142)7> rpc:call('hw@192.168.10.142',hw,store,[weather,cold]).
    cold
    (bibo@192.168.10.142)8> rpc:call('hw@192.168.10.142',hw,lookup,[weather]).
    {ok,cold}
    (bibo@192.168.10.142)9>

    步骤

    启动节点: erl -name bibo@192.168.10.142 -setcookie 4213

    启动进程: rpc:call('hw@192.168.10.142',hw,store,[weather,cold]).

                   rpc:call('hw@192.168.10.142',hw,lookup,[weather]).

    注意: 'hw@192.168.10.142'  不能少了单引号

    两个节点用的代码是同样的, 如下:

    %% @author Administrator
    %% @doc @todo Add description to bibo.
    
    
    -module(bibo).
    
    %% ====================================================================
    %% API functions
    %% ====================================================================
    -export([]).
    
    
    
    %% ====================================================================
    %% Internal functions
    %% ====================================================================
    
    %% ====================================================================
    %% API functions
    %% ====================================================================
    %%-export([start/2, stop/1]).
    -compile(export_all).
     
    
    %% ====================================================================
    %% Internal functions
    %% ====================================================================
    
    
    start()->
        register(kvs, spawn(fun()->
                                    loop()
                            end)
                ).
    
    loop()->
        receive
            {From, {store, Key, Value}}->
                put(Key, {ok, Value}),
                From ! {kvs, Value},
                loop();
            {From, {lookup, Key}} ->
                From ! {kvs, get(Key)},
                loop()
        end.
    
    store(Key, Value)->
        rpc({store, Key, Value}).
    
    lookup(Key)->
        rpc({lookup, Key}).
    
    rpc(Q)->
        kvs ! {self(), Q},
        receive
            {kvs, Reply}->
                Reply
        end.
            
        
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    你会卖掉自己的网上信息吗?大数据可能根本不属于你
    机器学习——TensorFLow实战房价预测
    数据库运作实践三三之歌(秘制口诀)
    1000行MySQL学习笔记,收藏版!
    吐血整理深度学习入门路线及导航【教学视频+大神博客+书籍整理】+【资源页】(2019年已经最后一个月了,你还不学深度学习吗???)
    Ubuntu Snap 简述
    参数传递
  • 原文地址:https://www.cnblogs.com/jluzhsai/p/4402210.html
Copyright © 2020-2023  润新知