jarrodhroberson
2/14/2014 - 3:38 AM

Python/Twisted vs Erlang/OTP

Python/Twisted vs Erlang/OTP

-module(linereceiver).
-export([start/1]).

sleep(T) ->
    receive
       after T ->
           true
    end.

start(Port) ->
    spawn(fun() ->
            start_parallel_server(Port),
            sleep(infinity)
          end).

start_parallel_server(Port) ->
    {ok, Listen} = gen_tcp:listen(Port, [binary, {packet,line},{reuseaddr, true},{active, true}]),
    spawn(fun() -> par_connect(Listen)end).

par_connect(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> par_connect(Listen) end),
    inet:setopts(Socket, [{packet, line}, list, {nodelay, true}, {active, true}]),
    io:format("Connection Made!~n"),
    get_line(Socket).

get_line(Socket) ->
    receive
        {tcp, Socket, Line} ->
            io:format("Received Line:~p~n", [Line]),
            get_line(Socket);
        {tcp_closed, Socket} ->
            io:format("Connection Closed!~n"),
            void
    end.