[erlang-questions] gen_tcp and processes receiving

James Cone <>
Fri Oct 5 07:44:38 CEST 2007

Message-ID: <>

Hello Stephan, et al,

I don't know how to reply to a digest message; please accept my 
apologies if this reply is not joined on.  I am using Thunderbird, and 
am open to suggestions by email.

I have got Stephan's code to work.  The whole code is below.  I made the 
following gratuitous changes:
   - make the host to connect to into a parameter

I had to make the following changes:
   - add {packet, raw} to the connect options
       - vaguely explained under recv on the gen_tcp page
       - explained better on the inet page

   - use the beginners' syntax for spawn_link
       - I have no idea why this is important, but I couldn't get the 
other one to go

   - complete the loops in doListen and receiver
       - just to get all of the characters

   - remove the io:format from doListen
       - there seems to be locking in the io, so that io from receiver 
can cause starvation in doListen

The code has the following remaining problems:
   - receiver ends disgracefully, when the other end closes the socket 

   - when receiver ends, it takes out doListen, so some of the received 
characters may not be processed

I am confused about Stephan's goal, and whether one byte is an 
appropriate buffer size; it may be for demonstration purposes only.

I am confused about whether there is a better way (documented in inet or 
somewhere) of causing TCP flow-control on the socket.




-export([start/1, receiverStart/2]).

start(Host) ->
     {ok, Socket} = gen_tcp:connect(Host, 80,
                                    [{active, false}, {packet, raw}]),

     gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
                  "Host: www.erlang.org\r\n"
                  "Connection: close\r\n"

     Pid = spawn_link(?MODULE, receiverStart, [self(), Socket]),

%    Pid = spawn_link(fun() ->
%                            receive
%                                go ->
%                                    ok
%                            end,
%                            receiver(self(), Socket)
%                    end),
     ok = gen_tcp:controlling_process(Socket, Pid),
     Pid ! go,


doListen(Socket) ->
         {byte, _B} ->
             io:format("Ok, I received a byte: ~p ~n", [[_B]])
     after 10000 ->

receiverStart(Listener, Socket) ->
     io:format("Starting with ~w, ~w~n", [Listener, Socket]),
       go -> ok
     receiver(Listener, Socket).

receiver(Listener, Socket) ->
%    io:format("Receiving...~n"),
     {ok, [B]} = gen_tcp:recv(Socket, 1),
     Listener ! {byte, B},
     receiver(Listener, Socket).

More information about the erlang-questions mailing list