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}.
原文地址:https://www.cnblogs.com/qq188380780/p/13502881.html