/jaerlang-code/code/socket_examples.erl

https://github.com/killme2008/erlib · Erlang · 94 lines · 69 code · 17 blank · 8 comment · 0 complexity · ed120282e3d8b216b31ac63b4a8cc615 MD5 · raw file

  1. %% ---
  2. %% Excerpted from "Programming Erlang",
  3. %% published by The Pragmatic Bookshelf.
  4. %% Copyrights apply to this code. It may not be used to create training material,
  5. %% courses, books, articles, and the like. Contact us if you are in doubt.
  6. %% We make no guarantees that this code is fit for any purpose.
  7. %% Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
  8. %%---
  9. -module(socket_examples).
  10. -compile(export_all).
  11. -import(lists, [reverse/1]).
  12. nano_get_url() ->
  13. nano_get_url("www.google.com").
  14. nano_get_url(Host) ->
  15. {ok,Socket} = gen_tcp:connect(Host,80,[binary, {packet, 0}]), %% (1)
  16. ok = gen_tcp:send(Socket, "GET / HTTP/1.0\r\n\r\n"), %% (2)
  17. receive_data(Socket, []).
  18. receive_data(Socket, SoFar) ->
  19. receive
  20. {tcp,Socket,Bin} -> %% (3)
  21. receive_data(Socket, [Bin|SoFar]);
  22. {tcp_closed,Socket} -> %% (4)
  23. list_to_binary(reverse(SoFar)) %% (5)
  24. end.
  25. nano_client_eval(Str) ->
  26. {ok, Socket} =
  27. gen_tcp:connect("localhost", 2345,
  28. [binary, {packet, 4}]),
  29. ok = gen_tcp:send(Socket, term_to_binary(Str)),
  30. receive
  31. {tcp,Socket,Bin} ->
  32. io:format("Client received binary = ~p~n",[Bin]),
  33. Val = binary_to_term(Bin),
  34. io:format("Client result = ~p~n",[Val]),
  35. gen_tcp:close(Socket)
  36. end.
  37. start_nano_server() ->
  38. {ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4}, %% (6)
  39. {reuseaddr, true},
  40. {active, true}]),
  41. {ok, Socket} = gen_tcp:accept(Listen), %% (7)
  42. gen_tcp:close(Listen), %% (8)
  43. loop(Socket).
  44. loop(Socket) ->
  45. receive
  46. {tcp, Socket, Bin} ->
  47. io:format("Server received binary = ~p~n",[Bin]),
  48. Str = binary_to_term(Bin), %% (9)
  49. io:format("Server (unpacked) ~p~n",[Str]),
  50. Reply = lib_misc:string2value(Str), %% (10)
  51. io:format("Server replying = ~p~n",[Reply]),
  52. gen_tcp:send(Socket, term_to_binary(Reply)), %% (11)
  53. loop(Socket);
  54. {tcp_closed, Socket} ->
  55. io:format("Server socket closed~n")
  56. end.
  57. error_test() ->
  58. spawn(fun() -> error_test_server() end),
  59. lib_misc:sleep(2000),
  60. {ok,Socket} = gen_tcp:connect("localhost",4321,[binary, {packet, 2}]),
  61. io:format("connected to:~p~n",[Socket]),
  62. gen_tcp:send(Socket, <<"123">>),
  63. receive
  64. Any ->
  65. io:format("Any=~p~n",[Any])
  66. end.
  67. error_test_server() ->
  68. {ok, Listen} = gen_tcp:listen(4321, [binary,{packet,2}]),
  69. {ok, Socket} = gen_tcp:accept(Listen),
  70. error_test_server_loop(Socket).
  71. error_test_server_loop(Socket) ->
  72. receive
  73. {tcp, Socket, Data} ->
  74. io:format("received:~p~n",[Data]),
  75. atom_to_list(Data),
  76. error_test_server_loop(Socket)
  77. end.