六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

gen_server Hello World

[复制链接]

升级  84.33%

137

主题

137

主题

137

主题

举人

Rank: 3Rank: 3

积分
453
 楼主| 发表于 2013-1-27 05:08:53 | 显示全部楼层 |阅读模式
简单的gen_server Hello World程序

代码:
-module(genserver).-behaviour(gen_server).-export([start/0, hello/1]).-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).start() ->   gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).hello(Name) ->  Reply = gen_server:call(?MODULE, {hello, Name}),  io:format("~s~n", [Reply]).handle_call({hello, Name}, _From, State) ->  Reply = lists:append(["Hello, ", Name, "!"]),  {reply, Reply, State}.init(_Args) ->  {ok, init}.handle_cast(_Request, State) ->  {noreply, State}.handle_info(_Info, State) ->  {noreplay, State}. terminate(_Reason, _State) ->  ok.code_change(_OldVsn, State, _Extra) ->  {ok, State}.

编译:
Eshell> c(genserver).

运行:
Eshell> genserver:start().Eshell> genserver:hello("Hideto").

输出结果:
Hello, Hideto!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表