einval error when gen_tcp:recv after successful gen_tcp:send

Sean Hinde <>
Thu Dec 9 23:52:06 CET 2004

On 9 Dec 2004, at 18:03,  wrote:

> On Thu, Dec 09, 2004 at 04:15:19PM +0100, Ericsson, Bjorn wrote:
>>    when the client tries to use gen_tcp:recv. This is where it 
>> receives
>>    {error, einval}.
>>    get_response(Request, Port) ->
>>    %%% Setup connection %%%
>>      case gen_tcp:connect("localhost", Port,
>>                                   [list, {packet, 0},{reuseaddr, 
>> true}])
> Your socket is (as they are by default) in active mode, this means
> that it's not allowed to call recv() on the socket, instead of
> actively recv()'ing your process will get messages on the form of:
>         {tcp, Socket, Data}
> So either you have tp specify {active, false} as a socket option
> when you create the socket, or rewrite your code to receive
> messages instead of actively recv() 'ing.
> recv() and {active , false} has the advantage that you
> get flow control for free (through TCP/IP) whereas
> {active, true} has the advantage that it's more flexible.
> Read erlang man pages for 'gen_tcp' and 'inet' several times.


One very nice way to use sockets is with the use of the now well 
documented {active, once} option. This allows you to get the benefit of 
flow control, but retain the nice semantics of packets being just 
another message into your process.

call inet:setopts(S, [{active, once}]) each time after receiving a 
packet when you are ready to receive the next one.


More information about the erlang-questions mailing list