gen_tcp bug ?

Raimo Niskanen raimo@REDACTED
Mon Jun 13 13:55:35 CEST 2005


I confirm this behaviour, and I regard it as a bug that I am about to
fix. Not being a socket guru myself I wonder, what is the expected
behaviour of gen_tcp:recv(Fd, N) when there are less than N
bytes in the receive buffer but more than 0?

a) To return with the available bytes.
b) To wait until there are N bytes, but if the socket gets closed
   or some error occurs return with the available bytes and let 
   subsequent calls indicate the error.

My guess is b) but I would like a second opinion.


klacke@REDACTED (Claes Wikstrom) writes:

> Is This a bug ? I surely consider it weird and unexpected.
> 
> Run a:srv() in one shell and then a:cli() in another?
> 
> I'd expect the call to gen_tcp:recv(Fd, 100) to return
> {ok, Bin} where size(Bin) == 50 in the second call. That is
> how unix read() works. As it is now, it's not good and severely
> violates the "principle of least surprise"
> 
> What do you think.
> 
> I realize that the prefered mode of operation is to do
> 
>     gen_tcp:recv(Fd, 0)
> 
> 
> But ......
> 
> 
> 
> /klacke
> 
> -module(a).
> -compile(export_all).
> 
> 
> 
> srv() ->
>     {ok, LS} = gen_tcp:listen(5678,[{active, false}, binary]),
>     {ok, A} = gen_tcp:accept(LS),
>     srv(A, gen_tcp:recv(A, 100)).
> 
> srv(Fd, {ok, Bin}) ->
>     io:format("Got ~p bytes\n", [size(Bin)]),
>     srv(Fd, gen_tcp:recv(Fd, 100));
> srv(Fd, Err) ->
>     io:format("ERR: ~p~n", [Err]).
> 
> 
> 
> 
> 
> cli() ->
>     {ok, Fd} = gen_tcp:connect(localhost, 5678, [{active, false}]),
>     gen_tcp:send(Fd, lists:duplicate(150, $A)),
>     gen_tcp:close(Fd).
> 
>     
> 

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list