Posted By

fuzzylollipop on 11/13/09


Tagged

python erlang twisted


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fuzzylollipop


twisted style line receiver server in erlang


 / Published in: Other
 

  1. -module(linereceiver).
  2.  
  3. -export([start/1]).
  4.  
  5. sleep(T) ->
  6. receive
  7. after T ->
  8. true
  9. end.
  10.  
  11. start(Port) ->
  12. spawn(fun() ->
  13. start_parallel_server(Port),
  14. sleep(infinity)
  15. end).
  16.  
  17. start_parallel_server(Port) ->
  18. {ok, Listen} = gen_tcp:listen(Port, [binary, {packet,line},{reuseaddr, true},{active, true}]),
  19. spawn(fun() -> par_connect(Listen)end).
  20.  
  21. par_connect(Listen) ->
  22. {ok, Socket} = gen_tcp:accept(Listen),
  23. spawn(fun() -> par_connect(Listen) end),
  24. inet:setopts(Socket, [{packet, line}, list, {nodelay, true}, {active, true}]),
  25. io:format("Connection Made!~n"),
  26. get_line(Socket).
  27.  
  28. get_line(Socket) ->
  29. receive
  30. {tcp, Socket, Line} ->
  31. io:format("Received Line:~p~n", [Line]),
  32. get_line(Socket);
  33. {tcp_closed, Socket} ->
  34. io:format("Connection Closed!~n"),
  35. void
  36. end.

Report this snippet  

You need to login to post a comment.