(2) Small project----establishing Erlang server

Source: Internet
Author: User

Originally intended to embed protobuf in quick under window, found a lot of errors. The study didn't work out all day. Can only take a rest under the Erlang service side, first to do a good job of the server and then back into the protobuf to quick.

Under window can not use rebar, can only manage themselves. Erlang is a weakness in itself. A simple server based on the OTP framework, is a standard implementation, and then slowly expand it.

(1). Set up a good directory doc,ebin,include,priv,src,testclient

(2). Add metadata Server.app in the Ebin directory to start the application

(3). Build Bz_server_sup.erl,bz_server_app.erl,bz_server.erl three files under SRC.

Bz_server_app.erl is used to package programs, start, shut down. Only START/2,STOP/1 two functions are available

Start (_starttype,_startargs)
{Ok,lsock} = Gen_tcp:listen (? Default_port, huh? Tcp_options),
Case Bz_server_sup:start_link (Lsock) of
{Ok,pid}
Bz_server_sup:start_child (),
{ok,pid};
Other
Io:format ("Bz_server_app Listen error: ~p!", [other]),
{Error,other}
End.


Stop (_state),
OK .

Bz_server_sup.erl Listener Top-level monitoring, set the sub-monitoring of the start

Start_link (Lsock)
Supervisor:start_link ({"127.0.0.1",?) SERVER},? Module,[lsock]).

Start_child ()
Supervisor:start_child (? Server,[]).


%%supervisor Callback
Init ([Lsock])
Server = {Bz_server, {bz_server, Start_link, [Lsock]},
Temporary, Brutal_kill, worker, [Bz_server]},
children = [Server],
Restartstrategy = {simple_one_for_one, 0, 1},
{OK, {restartstrategy, children}}.

Bz_server.erl. Gen_server, the majority of implementations that are responsible for waiting for a connection are implemented here. In the future, I intend to implement the message sent over by the client here in the distribution.

Percent-------------Private---------------------
Accept (Listensocket)
Case Gen_tcp:accept (Listensocket) of
{Ok,socket}
Io:format ("<<<<<<<<<<start connect>>>>>>>>> \ n"),
Io:format ("Accept client socket: ~p \ n", [socket]),
Gen_tcp:controlling_process (Socket, Self ()),
Bz_server_sup:start_child ();
{Error,reason}
Io:format ("Accept error reason: ~p \ n", [reason]),
Accept (Listensocket);
_res
Io:format ("Accept unexpected result: ~p \ n", [_res]),
Accept (Listensocket)
End.


Handle_info ({tcp,socket,rawdata},state)-percent data collection distribution
Io:format ("Socket: ~p, rawdata: ~p \ n", [Socket,rawdata]),
%%gen_tcp:send (Socket, "Erlang Server Bin"),
{Noreply,rawdata};

(2) Small project----establishing Erlang server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.