problem with write() to a socket and EPIPE

From: oliver.kowalke@t-online.de
Date: Mon Jun 18 2001 - 09:36:23 EST


Hello,

I've the following problem.
If the peer has closed its socket connection the second write to this
socket should return -1 and errno should be set to EPIPE (if SIGPIPE is
set
to be ignored). This never happens with my code. Why?

OS: Linux (Debian 2.2r3)
kernel: 2.4.4
compiler: gcc-2.95.2
c-lib: libc-2.1.3

with best regards,
Oliver

(writen() is a member function of my socket C++-class)

ssize_t
sock::writen( const void * vptr, size_t n)
{
        size_t nleft;
        ssize_t nwritten;
        const char *ptr;

        ptr = static_cast< char * >( vptr);
        nleft = n;

        struct sigaction new_sa;
        struct sigaction old_sa;
        
        new_sa.sa_handler = SIG_IGN;
        ::sigemptyset( & new_sa.sa_mask);
        new_sa.sa_flags = 0;
        ::sigaction( SIGPIPE, & new_sa, & old_sa);

        while ( nleft > 0)
        {
                if ( ( nwritten = ::write( m_handle, ptr, nleft) ) <=
0)
                {
                        if ( errno == EINTR)

                                nwritten = 0; /* and call
write() again */

                        else if ( errno == EPIPE)

                                return EOF; /* write to
socket with no readers */

                        else

                                throw net_io_ex( ::strerror( errno),
"writen()", __FILE__); /* error */

                }

                nleft -= nwritten;
                ptr += nwritten;
        }
        /* set to its previous action */
        ::sigaction( SIGPIPE, & old_sa, 0);

        return n;
}
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sat Jun 23 2001 - 21:00:21 EST