title: Erlang: gen_tcp:recv() does not receive packet sent from client?
body: <p>I modified <a href="https://stackoverflow.com/questions/15602314/erlang-server-cursor-hangs-on-command-line">this server</a> to use <code>gen_tcp:recv</code> inorder to limit the number of bytes for a packet to 50. I commented out the line <code>inet:setopts(Socket, [{active, once}]),</code> because <code>gen_tcp:recv</code> is supposed to be <code>{active,false}</code>. This is the client side erl shell</p>
  
  <pre><code>2&gt; cp3:client().
  exit
  3&gt; 
  </code></pre>
  
  <p>and this is the server side erl shell</p>
  
  <pre><code>4&gt; cp3:server().
  Started Server:
  &lt;0.46.0&gt;
  Accept Server:
  Pid &lt;0.48.0&gt;
  Connection accepted 
  Accept Server:
  Loop Server:
  5&gt; 
  </code></pre>
  
  <p>I also wondered how can I know if the socket closed with the return value <code>{tcp_closed, Socket}</code> if <code>gen_tcp:recv</code> doesn't create one?</p>
  
  <pre><code>-module(cp3).
  -export([client/0, server/0,start/0,accept/1,enter_loop/1,loop/1]).
  
  client() -&gt;
      {ok, Socket} =  gen_tcp:connect("localhost", 4001,[list, {packet, 0}]),
      ok = gen_tcp:send(Socket, "packet"),
      receive
          {tcp,Socket,String} -&gt;
              io:format("Client received = ~p~n",[String]),       
              io:format("Client result = ~p~n",[String]),
              gen_tcp:close(Socket)
          after 1000 -&gt;
              exit        
      end.
  
  server() -&gt; 
      Pid = spawn(fun()-&gt; start() end),
      Pid.
  
  start() -&gt;  
      io:format("Started Server:~n"),
      {ok, Socket} = gen_tcp:listen(4001, [binary, {packet, 0},{reuseaddr, true},{active, false}]),
      accept(Socket).
  
  accept(ListenSocket) -&gt;
      io:format("Accept Server:~n"),
      case gen_tcp:accept(ListenSocket) of
          {ok, Socket} -&gt;
              Pid = spawn(fun() -&gt;
                  io:format("Connection accepted ~n", []),
                  enter_loop(Socket)
              end),
              io:format("Pid ~p~n",[Pid]),
              gen_tcp:controlling_process(Socket, Pid),
              Pid ! ack,
              accept(ListenSocket);
          Error -&gt;
              exit(Error)
      end.
  
  enter_loop(Socket) -&gt;
      %% make sure to acknowledge owner rights transmission finished
      receive ack -&gt; ok end,
      loop(Socket).
  
  loop(Socket) -&gt;
      %% set socket options to receive messages directly into itself
      %%inet:setopts(Socket, [{active, once}]),
      io:format("Loop Server:~n"),
      case gen_tcp:recv(Socket, 50) of
      {ok, Data} -&gt;  
          case Data of
              &lt;&lt;"packet"&gt;&gt; -&gt;                 
                  io:format("Server replying = ~p~n",[Data]),   
                  gen_tcp:send(Socket, Data),
                  loop(Socket)                  
          end;
      {error, Reason} -&gt;      
          io:format("Error on socket ~p reason: ~p~n", [Socket, Reason])          
      end.
  </code></pre>