Skip to content

Instantly share code, notes, and snippets.

@yongboy
Created October 24, 2012 09:36
Show Gist options
  • Save yongboy/3945140 to your computer and use it in GitHub Desktop.
Save yongboy/3945140 to your computer and use it in GitHub Desktop.
tcp_server_app
-module(tcp_server_app).
-author('yongboy@gmail.com').
-behaviour(application).
-export([start/2, stop/1]).
-define(DEF_PORT, 2222).
start(_Type, _Args) ->
Opts = [binary, {packet, 2}, {reuseaddr, true},
{keepalive, true}, {backlog, 30}, {active, false}],
ListenPort = get_app_env(listen_port, ?DEF_PORT),
{ok, LSock} = gen_tcp:listen(ListenPort, Opts),
case tcp_server_sup:start_link(LSock) of
{ok, Pid} ->
tcp_server_sup:start_child(),
{ok, Pid};
Other ->
{error, Other}
end.
stop(_S) ->
ok.
get_app_env(Opt, Default) ->
case application:get_env(application:get_application(), Opt) of
{ok, Val} -> Val;
_ ->
case init:get_argument(Opt) of
[[Val | _]] -> Val;
error -> Default
end
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment