Why Socket Infinite Return -1 (Errno:104)

Why Socket Infinite Return -1 (Errno:104)

My socket program is a server-client structure.

Something strange, there have a low probability that when the TCP socket just construct(client connect and server accept).

and I use select than read the socket, it will return -1 and errno is 104(Connection reset by peer).

But I do not send anything neither server nor client, in a normal way it should block at select function(my select function did not set time-out limit), but the read function return -1.

Why it happen? Or how can I do can get more detail to debug this problem?

Thank you all.

Platform: ubuntu 13.04 64bits, gcc compiler

minimal version of my code:

server:

for(;;)
    {
        rset= allset;
        select(maxfd+1, &rset, NULL, NULL, NULL);

        //read socket
        if( FD_ISSET(fconn[i].fd, &rset) )
        {
            len=read(fconn[i].fd, &tcp_buf.b[4], PACKET_LENGTH);

            //the connection will close
            if( len<0 )
            {
                printf("%s, fconn %s read length:%d, errno:%d(%s), close connection\n", crtTime(), fconn[i].ip, len, errno, strerror(errno) );
                close( fconn[i].fd );
                FD_CLR( fconn[i].fd, &allset );
            }
        }

        //construct new connection
        if( FD_ISSET(forwardSockfd, &rset) )
        {
            clilen= sizeof(cliaddr);
            connfd= accept( forwardSockfd, (struct sockaddr*)&cliaddr, &clilen );
            inet_ntop( AF_INET, &cliaddr.sin_addr, ip, sizeof(ip) );
            client_port= (int)cliaddr.sin_port;

            //set send buffer size
            int sendBuf= SOCKET_BUF_LENGTH;
            setsockopt(connfd, SOL_SOCKET, SO_SNDBUF, &sendBuf, sizeof(sendBuf));

            FD_SET( connfd, &allset );
        }
    }

client (my read function):

int readline(int fd, void* ptr, int len)
{
    int    rtn= len;
    void*  bp= ptr;
    int    rc;
    fd_set fdset;

    FD_ZERO(&fdset);
    FD_SET(fd, &fdset);
    while( len>0 )
    {
        select(fd+1, &fdset, NULL, NULL, NULL);
        rc=read(fd, bp, len);


        if(rc>0)
        {
            bp+= rc;
            len-= rc;
        }

        else
        {
            return rc;
        }
    }

    return rtn;
}
2

1 Answer

it will return -1 and errno is 104(Connection reset by peer)

So the connection was reset by the peer. That can happen any time. It's out of your control. The correct action is to close the socket and forget about it. The connection no longer exists. Trying to select and read from it again is just misplaced optimism.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.