gen_tcp bug ?

Chandrashekhar Mullaparthi chandrashekhar.mullaparthi@REDACTED
Tue Jun 14 00:56:46 CEST 2005


On 13 Jun 2005, at 12:55, Raimo Niskanen wrote:

> 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.
>

The best compromise is to probably introduce a new return value as 
Bengt suggested - {error, closed, Data}. This also avoids the problem 
which Per raised. Or in the case of 'active' sockets, a {tcp_closed, 
Socket, Data} message.

The same problem exists when the option {active, once} or {active, 
true} is set and the {packet, PacketType} mode is used?

-module(a).
-compile(export_all).

srv1() ->
     {ok, LS} = gen_tcp:listen(5678,[{active, true}, binary, {packet, 
line}]),
     {ok, A} = gen_tcp:accept(LS),
     srv1_loop(A).

srv1_loop(A) ->
     receive
	{tcp, A, Bin} ->
	    io:format("Got ~p bytes\n", [size(Bin)]),
	    srv1_loop(A);
	{tcp_closed, A} ->
	    io:format("Socket closed...~n", []);
	Err ->
	    io:format("Recvd ~p~n", [Err])
     end.


cli() ->
     {ok, Fd} = gen_tcp:connect(localhost, 5678, [{active, false}]),
     gen_tcp:send(Fd, lists:duplicate(150, $A)),
     gen_tcp:close(Fd).

cheers
Chandru





More information about the erlang-questions mailing list