title: How to set SSL_connect on non blocking socket with select on linux platform
body: <p>I am trying to use the select function to have non-blocking connection from a client to server.I found a tutorial with some code and tried to adapt to this:</p>
  
  <pre><code>...
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  err = connect(sockfd,(struct sockaddr*)&amp;sa,sizeof(sa));
  ...
  SSL_set_fd(pssl,sockfd);
  err = SSL_connect_nonb(pssl,sockfd,60);
  if(err &lt;=0 ){
      printf("SSL_connect:%s\n",ERR_error_string(SSL_get_error(pssl,err),NULL));
      return -1;
  }
  ...
  </code></pre>
  
  <p>The SSL_connect_nonb function is defined as bellow:</p>
  
  <pre><code>int SSL_connect_nonb(SSL*pssl,int sockfd, int nsec)
  {
      int flags, error;
      socklen_t len;
      fd_set rset, wset;
      struct timeval tval;
      flags = fcntl(sockfd, F_GETFL, 0);
      fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
      int err = SSL_connect(pssl);
      int err2 = SSL_get_error(pssl,err);
      switch(err2) {
              default:
                  printf("SSL_connect err=%s\n",ERR_error_string(err2,0));
                  return -1;
                  break;
              case SSL_ERROR_WANT_READ:
              case SSL_ERROR_WANT_WRITE:
                  break;
      }
      FD_ZERO(&amp;rset);
      FD_ZERO(&amp;wset);
      FD_SET(sockfd, &amp;rset);
      FD_SET(sockfd, &amp;wset);
      tval.tv_sec = nsec;
      tval.tv_usec = 0;
      if (select(sockfd+1, &amp;rset, &amp;wset, NULL,nsec ? &amp;tval:NULL) == 0) {
          return -1;
      }
      if(FD_ISSET(sockfd,&amp;rset) || FD_ISSET(sockfd, &amp;wset )) {
          len = sizeof(error);
          if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &amp;error, &amp;len) &lt; 0){
              return -1;
          }
      }else{
          printf("sockfd  not set\n");
          return -1;
      }
      fcntl(sockfd, F_SETFL, flags);
      if (error) {
          return -1;
      }
      return 1;
  }
  </code></pre>
  
  <p>The sockfd is correct with connect，the problem is that in SSL_connect_nonb the select function return avalue=1 (actually the condition FD_ISSET(sockfd, &amp;wset) is successful all time),but if I use blocking method as this :</p>
  
  <pre><code>....    
  SSL_set_fd(pssl,sockfd);
  err = SSL_connect(pssl);
  if(err &lt;=0 ){
      printf("SSL_connect:%s\n",ERR_error_string(SSL_get_error(pssl,err),NULL));
      return -1;
  }
  ...
  </code></pre>
  
  <p>the value of err is 0 because the SSL_connect is not successful, so, how to do with SSL_connect on non blocking socket by calling select function?</p>