Erlang echoserver

This commit is contained in:
Tony Garnock-Jones 2014-05-04 16:36:59 -04:00
parent 6cfade9e25
commit 61b9b55d74
1 changed files with 25 additions and 0 deletions

25
echoserver.erl Normal file
View File

@ -0,0 +1,25 @@
-module(echoserver).
-export([start/0]).
start() ->
{ok, LSock} = gen_tcp:listen(5999, [{active, true}, {packet, line}, {reuseaddr, true}]),
accept_loop(LSock).
accept_loop(LSock) ->
case gen_tcp:accept(LSock) of
{ok, Sock} ->
gen_tcp:controlling_process(Sock, spawn(fun () -> connection(Sock) end)),
accept_loop(LSock)
end.
connection(Sock) ->
receive
{tcp, _, Line} ->
gen_tcp:send(Sock, Line),
connection(Sock);
{tcp_closed, _} ->
ok;
Other ->
error_logger:error_report({connection, unhandled, Other})
end.