Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. -module(echo).
  2. -export([listen/1]).
  3.  
  4. -define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]).
  5.  
  6. % Call echo:listen(Port) to start the service.
  7. listen(Port) ->
  8. {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
  9. accept(LSocket).
  10.  
  11. % Wait for incoming connections and spawn the echo loop when we get one.
  12. accept(LSocket) ->
  13. {ok, Socket} = gen_tcp:accept(LSocket),
  14. spawn(fun() -> loop(Socket) end),
  15. accept(LSocket).
  16.  
  17. % Echo back whatever data we receive on Socket.
  18. loop(Socket) ->
  19. case gen_tcp:recv(Socket, 0) of
  20. {ok, Data} ->
  21. gen_tcp:send(Socket, Data),
  22. loop(Socket);
  23. {error, closed} ->
  24. ok
  25. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement