[Live-devel] Detecting socket close when receiving streams via TCP

Barry Stump bstump at codemass.com
Thu Jul 19 14:37:18 PDT 2012


My apologies for taking so long to reply, OSCON has been taking up all my
time this week.


> I *might* be able to modify the "readSocket()" function to better handle
> this case, but I don't want to risk inadvertently messing up higher-level
> code that uses this function by mishandling cases when "recvfrom()" might
> return zero in a *non*-error situation.
>
> In the situation that you are concerned about - recvfrom() returning "zero
> if the connection is TCP and the peer has closed its half side of the
> connection" - what is the value of "errno" in this case?  (If it's
> something non-zero, then I should be able to update "readSocket()" to add a
> test for this.)
>

By design, errno is only set when the return value is negative.  Socket
close (EOF) is not considered an error, and is indicated instead by a zero
return value.  This is the only use of a zero return value for TCP sockets
on POSIX.

Here's an example of Apache HTTPD relying on zero return value to detect
socket close/EOF (taken from
http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sendrecv.c?view=markup)

apr_status_t apr_socket_recvfrom(apr_sockaddr_t *from, apr_socket_t *sock,
                                 apr_int32_t flags, char *buf,
                                 apr_size_t *len)
{

// ...snip...

    do {
        rv = recvfrom(sock->socketdes, buf, (*len), flags,
                      (struct sockaddr*)&from->sa, &from->salen);
    } while (rv == -1 && errno == EINTR);

// ...snip...

    if (rv == 0 && sock->type == SOCK_STREAM) {
        return APR_EOF;
    }

    return APR_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20120719/c05d76a2/attachment.html>


More information about the live-devel mailing list