Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (35 sloc) 1.29 KB
%% module name must be the same as the filename
%% do_echo exported for using erlang:spawn/3
-export([do_echo/1, listen/1]).
%% TCP options for our listening socket. The initial list atom
%% specifies that we should receive data as lists of bytes (ie
%% strings) rather than binary objects and the rest are explained
%% better in the Erlang docs than I can do here.
{packet, 0},
{active, false},
{backlog, 1024}, % speeding up (default is 5)
{reuseaddr, true}]).
%% Listen on the given port, accept the first incoming connection and
%% launch the echo loop on it.
listen(Port) ->
{ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
%% The accept gets its own function so we can loop easily. Yay tail
%% recursion!
do_accept(LSocket) ->
{ok, Socket} = gen_tcp:accept(LSocket),
spawn(?MODULE, do_echo, [Socket]),
%% Sit in a loop, echoing everything that comes in on the socket.
%% Exits cleanly on client disconnect.
do_echo(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
gen_tcp:send(Socket, Data),
{error, closed} ->