title: Why socket infinite return -1 (errno:104)
body: <p>My socket program is a server-client structure.</p>
  
  <p>Something strange, there have a low probability that when the TCP socket just construct(client connect and server accept).</p>
  
  <p>and I use select than read the socket, it will return -1 and errno is 104(Connection reset by peer).</p>
  
  <p>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.</p>
  
  <p>Why it happen? Or how can I do can get more detail to debug this problem?</p>
  
  <p>Thank you all.</p>
  
  <p>Platform: ubuntu 13.04 64bits, gcc compiler</p>
  
  <p>minimal version of my code:</p>
  
  <p>server:</p>
  
  <pre><code>for(;;)
      {
          rset= allset;
          select(maxfd+1, &amp;rset, NULL, NULL, NULL);
  
          //read socket
          if( FD_ISSET(fconn[i].fd, &amp;rset) )
          {
              len=read(fconn[i].fd, &amp;tcp_buf.b[4], PACKET_LENGTH);
  
              //the connection will close
              if( len&lt;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, &amp;allset );
              }
          }
  
          //construct new connection
          if( FD_ISSET(forwardSockfd, &amp;rset) )
          {
              clilen= sizeof(cliaddr);
              connfd= accept( forwardSockfd, (struct sockaddr*)&amp;cliaddr, &amp;clilen );
              inet_ntop( AF_INET, &amp;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, &amp;sendBuf, sizeof(sendBuf));
  
              FD_SET( connfd, &amp;allset );
          }
      }
  </code></pre>
  
  <p>client (my read function):</p>
  
  <pre><code>int readline(int fd, void* ptr, int len)
  {
      int    rtn= len;
      void*  bp= ptr;
      int    rc;
      fd_set fdset;
  
      FD_ZERO(&amp;fdset);
      FD_SET(fd, &amp;fdset);
      while( len&gt;0 )
      {
          select(fd+1, &amp;fdset, NULL, NULL, NULL);
          rc=read(fd, bp, len);
  
  
          if(rc&gt;0)
          {
              bp+= rc;
              len-= rc;
          }
  
          else
          {
              return rc;
          }
      }
  
      return rtn;
  }
  </code></pre>