(2) small project ---- create an erlang server and an erlang Server
Originally intended to embed protobuf in quick in window, many errors were found. I did not do a good job of research for a day. You can only take a rest to get down to the erlang server. First, do a good job on the server and then embed protobuf into quick.
Rebar cannot be used in a window and can only be managed by itself. Erlang is your own weakness. A simple server based on the OTP framework is a standard implementation and will be expanded later.
(1). Create the directories doc, ebin, include, priv, src, and testClient.
(2) Add the metadata server. app to the ebin directory to start the application.
(3) create bz_server_sup.erl, bz_server_app.erl, and bz_server.erl files under src.
Bz_server_app.erl is used to package the program, start and close it. Only start/2 and stop/1 functions are provided.
Start (_ StartType, _ StartArgs)->
{OK, LSock} = gen_tcp: listen (? DEFAULT_PORT ,? TCP_OPTIONS ),
Case bz_server_sup: start_link (LSock)
{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 top-level listener, which specifies the start of the sublistener
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, which is responsible for most implementations after connection. In the future, we plan to distribute the messages sent from the client.
% ------------- Private ---------------------
Accept (ListenSocket)->
Case gen_tcp: accept (ListenSocket)
{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)-> % data collection and distribution
Io: format ("Socket :~ P, RawData :~ P \ n ", [Socket, RawData]),
% Gen_tcp: send (Socket, "erlang server bin "),
{Noreply, RawData };