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}.
    
  • 相关阅读:
    淘宝API学习之道:淘宝TOP之API接口接入教程
    hdu 2952 Counting Sheep
    【Java 虚拟机探索之路系列】:JIT编译器
    Android Afinal框架学习(一) FinalDb 数据库操作
    class、interface、struct的差别
    13个Cat命令管理文件实例汇总
    Linux df 命令用法示例
    九个uname命令获取Linux系统详情的实例
    Linux中查看进程的多线程
    精通Linux的“kill”命令
  • 原文地址:https://www.cnblogs.com/posase/p/13502881.html
Copyright © 2011-2022 走看看