-
Notifications
You must be signed in to change notification settings - Fork 0
/
code007.erl
35 lines (28 loc) · 835 Bytes
/
code007.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
-module(my_server).
-export([start/0]).
start() ->
{ok, ListenSocket} = gen_tcp:listen(8080, [{active, false}, {reuseaddr, true}]),
accept_clients(ListenSocket).
accept_clients(ListenSocket) ->
{ok, ClientSocket} = gen_tcp:accept(ListenSocket),
spawn(fun() -> handle_client(ClientSocket) end),
accept_clients(ListenSocket).
handle_client(Socket) ->
loop(Socket).
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
handle_data(Socket, Data),
loop(Socket);
{error, _} ->
gen_tcp:close(Socket)
end.
handle_data(Socket, Data) ->
% process the received data
io:format("Received: ~s~n", [Data]),
% send a response
gen_tcp:send(Socket, "OK").
% to start server
% 1> c(my_server).
% {ok,my_server}
% 2> my_server:start().