-
Notifications
You must be signed in to change notification settings - Fork 28
/
server_erlang.erl
44 lines (35 loc) · 1.29 KB
/
server_erlang.erl
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
32
33
34
35
36
37
38
39
40
41
42
43
44
%% http://jerith.za.net/writings/erlangsockettut.html
%% module name must be the same as the filename
-module(server_erlang).
%% 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.
-define(TCP_OPTIONS,[list,
{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),
do_accept(LSocket).
%% 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]),
do_accept(LSocket).
%% 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),
do_echo(Socket);
{error, closed} ->
ok
end.