Advertisement
Guest User

erlang test

a guest
Mar 28th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.69 KB | None | 0 0
  1. -module(test).
  2. -export([start/2]).
  3.  
  4. start(Address, Port) ->
  5.     OP = [
  6.         inet,
  7.         binary,
  8.         {reuseaddr, true},
  9.         {nodelay, true}
  10.     ],
  11.  
  12.     Request = ["GET / HTTP/1.1\r\nHost: ", Address, "\r\n\r\n"],
  13.     io:format("~p~n", [list_to_binary(Request)]),
  14.  
  15.     {ok, Socket} = gen_tcp:connect(Address, Port, OP, 5000),
  16.     ok = gen_tcp:send(Socket, list_to_binary(Request)),
  17.  
  18.     getdata(Socket, []).
  19.  
  20.  
  21. getdata(Socket, D) ->
  22.     io:format("receive data...~n", []),
  23.     receive
  24.         {tcp,Socket, Data} ->
  25.             getdata(Socket, [Data | D]);
  26.         {tcp_closed, Socket} ->
  27.             io:format("tcp_closed~n", []),
  28.             lists:reverse(D);
  29.         {tcp_error, Socket, _Reason} ->
  30.             io:format("tcp_error~n", []),
  31.             lists:reverse(D)
  32.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement