gen_tcp:recv - last segement when closed port

Per Hedeland <>
Sun Mar 12 16:44:47 CET 2006


Corrado Santoro <> wrote:
>
>as far as I remember, sometimes ago I had a similar problem in a C 
>program, so, even if Per argue that TCP promises a reliable byte stream 
>(and this is true), I think that there is a sort of strange behaviour in 
>handling disconnection conditions (I experienced this problem on Linux). 
>I solved the problem by readling a byte at time, but undoubtedly this is 
>not an elegant solution.

And sometimes we come up with "solutions" that just happen to avoid the
problem, which frequently leads to incorrect conclusions about what the
problem really was, not to mention the incorrect conclusion that what we
came up with actually *was* a *solution*...

>It could be related to the fact that, if you have 300 final bytes in the 
>buffer and perform a read requesting 1024 bytes, the socket 
>implementation says "no, I can't give you the buffer you request because 
>the socket has been closed".

Are you talking about Erlang sockets or C/Unix/POSIX ones now? What you
describe is the semantics of gen_tcp:recv() (see the thread I referenced
earlier), but for the C/Unix/POSIX socket API there is certainly no such
"fact". If there are 300 bytes available, and you ask for 1024, you will
get the 300 regardless of whether the connection has been closed or not
(the *socket file descriptor* will of course not be closed until you
actively close() it).

--Per



More information about the erlang-questions mailing list