Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

erlang test

By: a guest on Mar 28th, 2013  |  syntax: Erlang  |  size: 0.69 KB  |  views: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.
clone this paste RAW Paste Data