zoukankan      html  css  js  c++  java
  • Erlang TCP 实例

    Server

    -module(server).
    
    -behaviour(gen_server).
    
    %% API
    -export([
             start_link/0,
             stop/0
             ]).
    
    %% gen_server callbacks
    -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
             terminate/2, code_change/3]).
    
    -define(PORT, 1234).
    -define(HOST, {127,0,0,1}).
    
    -record(state, {port, sock, count=0}).
    
    %%%===================================================================
    %%% Maintenance API
    %%%===================================================================
    
    
    start_link([Port]) ->
        gen_server:start_link({local, ?MODULE}, ?MODULE, [Port], []).
    start_link() ->
        start_link([?PORT]).
    
    stop() ->
        io:format("Stop"),
        gen_server:cast(?MODULE, stop).
    
    %%%===================================================================
    %%% Services API
    %%%===================================================================
    
    %%%===================================================================
    %%% gen_server callbacks
    %%%===================================================================
    
    init([Port]) ->
        {ok, Listen} = gen_tcp:listen(Port, [binary, {active, true}]),
        {ok, #state{port = Port, sock = Listen}, 0}.
    
    handle_call(Msg, _Form, State) ->
        io:format("~p~n",[Msg]),
        {reply, State}.
    
    handle_cast(stop, State) ->
        {stop, normal, State}.
    
    handle_info({tcp, Socket, RawData}, State) ->
    
        Term = binary_to_term(RawData),
        case Term of
            stop -> 
                gen_tcp:send(Socket, <<"Stop">>),
                stop();
            _ -> 
                io:format("~p~n", [Term]),
                gen_tcp:send(Socket, <<"Success">>)
        end,
        {noreply, State};
    
    handle_info(timeout, State) ->
        io:format("timeout"),
        {ok, _Sock} = gen_tcp:accept(State#state.sock),
        {noreply, State}.
    
    terminate(_Reason, _State) ->
        ok.
    
    code_change(_OldVsn, State, _Extra) ->
        {ok, State}.
    

    Client

    -module(server).
    
    -behaviour(gen_server).
    
    %% API
    -export([
             start_link/0,
             stop/0
             ]).
    
    %% gen_server callbacks
    -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
             terminate/2, code_change/3]).
    
    -define(PORT, 1234).
    -define(HOST, {127,0,0,1}).
    
    -record(state, {port, sock, count=0}).
    
    %%%===================================================================
    %%% Maintenance API
    %%%===================================================================
    
    
    start_link([Port]) ->
        gen_server:start_link({local, ?MODULE}, ?MODULE, [Port], []).
    start_link() ->
        start_link([?PORT]).
    
    stop() ->
        io:format("Stop"),
        gen_server:cast(?MODULE, stop).
    
    %%%===================================================================
    %%% Services API
    %%%===================================================================
    
    %%%===================================================================
    %%% gen_server callbacks
    %%%===================================================================
    
    init([Port]) ->
        {ok, Listen} = gen_tcp:listen(Port, [binary, {active, true}]),
        {ok, #state{port = Port, sock = Listen}, 0}.
    
    handle_call(Msg, _Form, State) ->
        io:format("~p~n",[Msg]),
        {reply, State}.
    
    handle_cast(stop, State) ->
        {stop, normal, State}.
    
    handle_info({tcp, Socket, RawData}, State) ->
    
        Term = binary_to_term(RawData),
        case Term of
            stop -> 
                gen_tcp:send(Socket, <<"Stop">>),
                stop();
            _ -> 
                io:format("~p~n", [Term]),
                gen_tcp:send(Socket, <<"Success">>)
        end,
        {noreply, State};
    
    handle_info(timeout, State) ->
        io:format("timeout"),
        {ok, _Sock} = gen_tcp:accept(State#state.sock),
        {noreply, State}.
    
    terminate(_Reason, _State) ->
        ok.
    
    code_change(_OldVsn, State, _Extra) ->
        {ok, State}.
    
  • 相关阅读:
    erlang调试技术之etop
    erlang进程与操作系统线程
    BFS算法入门--POJ3984
    Linux学习笔记(2)Linux学习注意事项
    Linux学习笔记(1)Linux虚拟机安装过程中的知识点及常用管理工具
    题解 UVA10587 【Mayor's posters】
    【BZOJ4590】自动刷题机
    【Usaco2006Mar】Milk Team Select产奶比赛
    【区间DP】释放囚犯
    ssm项目中KindEditor的图片上传插件,浏览器兼容性问题
  • 原文地址:https://www.cnblogs.com/posase/p/13502881.html
Copyright © 2011-2022 走看看