博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的erlang TCP服务器
阅读量:6111 次
发布时间:2019-06-21

本文共 3955 字,大约阅读时间需要 13 分钟。

hot3.png

不解释,直接贴代码。

1) socket_options.hrl

-record(socket_options,        {port=8024,         loop,         name,         ip=any,         nodelay=false,         backlog=128,         listen=null,                         active_sockets=0}		).

2)  test_server.erl

-module(test_server).-export([start/0, stop/0, looper/1]).-include("socket_options.hrl").%开启服务start()->  test_socket_server:start(#socket_options{loop = {?MODULE, looper}}).%关闭服务stop()->  test_socket_server:stop(). looper(Socket) ->    inet:setopts(Socket, [{active, once}]),    receive    {tcp, Socket, Data} ->        io:format("Got packet: ~p~n", [Data]),        gen_tcp:send(Socket, Data),        looper(Socket);    {tcp_closed, Socket}->        io:format("Socket ~p closed~n", [Socket]);    {tcp_error, Socket, Reason} ->        io:format("Error on socket ~p reason: ~p~n", [Socket, Reason])    end.

3) test_socket_server.erl

-module(test_socket_server).-export([start/0, start/1, stop/0, stop/1]).-export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3,         handle_info/2]).-include("socket_options.hrl").-define(RECBUF_SIZE, 8192).start() ->	gen_server:start(?MODULE, [], []).start(InitArg = #socket_options{name = Name}) ->    case Name of        undefined ->	    gen_server:start({local,?MODULE}, ?MODULE, InitArg, []);        _ ->            gen_server:start({local,Name}, ?MODULE, InitArg, [])	end.stop() ->  gen_server:cast(?MODULE, stop).stop(Name) when is_atom(Name) ->    gen_server:cast(Name, stop);stop(Pid) when is_pid(Pid) ->    gen_server:cast(Pid, stop);stop(_State = #socket_options{name = Name}) ->    stop(Name).%回调函数%init(State = #socket_options{ip = Ip, port = Port, backlog = Backlog, nodelay = NoDelay}) ->init(State = #socket_options{port = Port, backlog = Backlog, nodelay = NoDelay}) ->    BaseOpts = [binary,        {reuseaddr, true},        {packet, 0},        {backlog, Backlog},        {recbuf, ?RECBUF_SIZE},        {active, false},        {nodelay, NoDelay}],    listen(Port, BaseOpts, State).    %%监听         handle_call(_Msg, _Caller, State) -> {noreply, State}.	handle_info(_Msg, Library) -> {noreply, Library}.terminate(Reason, #socket_options{listen = Listen}) ->    io:format("socket close,~p~n",[Reason]),    gen_tcp:close(Listen).             %%关闭Listencode_change(_OldVsn, State, _Extra) ->    State.handle_cast({accepted, _Pid}, State = #socket_options{active_sockets = ActiveSockets,             listen = Listen, loop = Loop}) ->    NewState = State#socket_options{active_sockets = 1 + ActiveSockets},    test_socket_server_acceptor:start_link(self(), Listen, Loop),    {noreply, NewState}; handle_cast(stop, Name) ->     %io:format("关闭服务器!~n"),    {stop, normal , Name}. listen(Port, Opts, State) ->    case gen_tcp:listen(Port, Opts) of        {ok, Listen} ->			test_socket_server_acceptor:start_link(self(), Listen, State#socket_options.loop),			NewState = State#socket_options{listen = Listen},			{ok, NewState};        {error, Reason} ->            {stop, Reason}    end.
 4) test_socket_server_acceptor.erl

-module(test_socket_server_acceptor).-export([start_link/3, init/3]).start_link(Server, Listen, Loop) ->    proc_lib:spawn_link(?MODULE, init, [Server, Listen, Loop]).init(Server, Listen, Loop) ->    case catch gen_tcp:accept(Listen) of        {ok, Socket} ->            gen_server:cast(Server, {accepted, self()}),            call_loop(Loop, Socket);        {error, closed} ->            exit(normal);        {error, timeout} ->            init(Server, Listen, Loop);        {error, esslaccept} ->            exit(normal);        _ ->            exit({error, accept_failed})    end.call_loop({M, F}, Socket) ->    io:format("socket call_loop,~n"),    M:F(Socket);call_loop({M, F, [A1]}, Socket) ->    M:F(Socket, A1);call_loop({M, F, A}, Socket) ->    erlang:apply(M, F, [Socket | A]);call_loop(Loop, Socket) ->    Loop(Socket).
下一步是想在此基础上写一个聊天室什么的,不过我目前我想先实践完OTP实战这本书。

转载于:https://my.oschina.net/huanghongqiao/blog/138875

你可能感兴趣的文章
Porter/Duff,图片加遮罩setColorFilter
查看>>
黄聪:VMware安装Ubuntu10.10【图解】转
查看>>
Centos 6.x 升级openssh版本
查看>>
公式推♂倒题
查看>>
vue实现点击展开,点击收起
查看>>
如何使frame能居中显示
查看>>
第k小数
查看>>
构建之法阅读笔记三
查看>>
Python/PHP 远程文件/图片 下载
查看>>
【原创】一文彻底搞懂安卓WebView白名单校验
查看>>
写给对前途迷茫的朋友:五句话定会改变你的人生
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
JAVA入门到精通-第86讲-半双工/全双工
查看>>
bulk
查看>>
js document.activeElement 获得焦点的元素
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>
day6-if,while,for的快速掌握
查看>>
JavaWeb学习笔记(十四)--JSP语法
查看>>
【算法笔记】多线程斐波那契数列
查看>>