summaryrefslogtreecommitdiff
path: root/src/mochiweb/mochiweb_echo.erl
blob: f32d6803b99a993d5324ca528f75258b6677481c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
%% @author Bob Ippolito <bob@mochimedia.com>
%% @copyright 2007 Mochi Media, Inc.

%% @doc Simple and stupid echo server to demo mochiweb_socket_server.

-module(mochiweb_echo).
-author('bob@mochimedia.com').
-export([start/0, stop/0, loop/1]).

stop() ->
    mochiweb_socket_server:stop(?MODULE).

start() ->
    mochiweb_socket_server:start([{name, ?MODULE},
                                  {port, 6789},
                                  {ip, "127.0.0.1"},
                                  {max, 1},
                                  {loop, {?MODULE, loop}}]).

loop(Socket) ->
    case gen_tcp:recv(Socket, 0, 30000) of
        {ok, Data} ->
            case gen_tcp:send(Socket, Data) of
                ok ->
                    loop(Socket);
                _ ->
                    exit(normal)
            end;
        _Other ->
            exit(normal)
    end.