% Ked sa server pusti na altairi a klient na notebook-u, tak toto v celku pekne funguje. Klienta je mozne spustit viackrat. Zakazdym sa ale fork-ne iny worker.

-module(server).
-export([start/0, worker/1]).

start() ->
  {ok, LSock} = gen_tcp:listen(2000, [binary, {packet, 0}, {active, false}]),
  server(LSock).

server(LSock) ->
  {ok, Sock} = gen_tcp:accept(LSock),
  spawn(?MODULE, worker, [Sock]),
  server(LSock).

% This process is responsible for handling single client.
worker(Sock) ->
  {ok, Bin} = gen_tcp:recv(Sock, 0),
  {Num1, Num2} = binary_to_term(Bin),
  io:format("numbers: ~w ~w~n", [Num1, Num2]),
  io:format("sum: ~w~n", [Num1+Num2]),
  gen_tcp:close(Sock).
