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}.
    
  • 相关阅读:
    oracle连接命令
    oracle Wrap加密
    oracle copy
    oracle loader
    oracle一些常见的问题
    python-cn(华蟒用户组,CPyUG 邮件列表)
    代理服务器验证工具
    多线程中的信号/槽
    【多线程】python界面阻塞,白屏,not responding解决的简单例子
    vi命令
  • 原文地址:https://www.cnblogs.com/qq188380780/p/13502881.html
Copyright © 2011-2022 走看看