Skip to content

Instantly share code, notes, and snippets.

@blt

blt/echo.erl

Created Jul 19, 2010
Embed
What would you like to do?
-module(echo).
-export([listen/1, accept/1]).
-define(TCP_OPTIONS, [binary, {active, false}]).
listen(Port) ->
{ok, Socket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
accept(Socket).
accept(LSocket) ->
{ok, Socket} = gen_tcp:accept(LSocket),
spawn(?MODULE, accept, [LSocket]),
loop(Socket, []).
loop(Socket, BList) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
loop(Socket, [Data|BList]);
{error, closed} ->
gen_tcp:send(Socket, lists:reverse(BList)),
ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment