cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: connect failed if set socket non-blocking,why?

From: Andy Hobbs <andy_at_hobbs.uk.net>
Date: Mon, 25 Oct 2004 11:55:48 +0100

Hi

Are you using threads?

if so are you using thread safe errno? you need to specify

_REENTRANT
and
_LIBC_REENTRANT

when compiling

Here is a section of my code that handles this:

 if ((n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0)
   {
      if (errno != EINPROGRESS)
      {
         errprint("sockError after connect %d, tid %d", errno,
pthread_self());
         close(sockfd);
         if (sockError == EINTR)
            sockError = ETIMEDOUT;
      }
      else
      {
         // Call select to wait for writability or timeout or error
         // use getsockopt to find any errors from select
         select(sockfd + 1, NULL, &wrset, NULL, &timeout);
         
         // The descriptor should now be writable unless there was a
timeout
         len = sizeof(sockError);
         getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &sockError, &len);
         
         if (sockError != 0)
            errprint("sockError %d not 0 %d, %d", sockfd, sockError,
errno);
         
         n = sockError;
      }
   }

Andy

On Mon, 2004-10-25 at 11:17, 黄志军 wrote:
> ----- Original Message -----
> From: "Andy Hobbs" <andy_at_hobbs.uk.net>
> To: "libcurl development" <curl-library_at_cool.haxx.se>
> Sent: Monday, October 25, 2004 5:45 PM
> Subject: Re: connect failed if set socket non-blocking,why?
>
>
> > Hi,
> >
> > connect returns instantly when the socket is non-blocking, you need to
> > call select() and wait for the connect to complete.
>
> connect return value is -1 ,but errno is also 0, that is the key reason puzzling me.
> >
> > Andy
> >
> >
> > On Mon, 2004-10-25 at 09:57, 黄志军 wrote:
> > > I link my program with static library libcurl.a and find it always
> > > return CURLE_COULDNT_CONNECT.
> > >
> > > if i ignore function Curl_nonblock() in connect.c( curl-7.12.2), the
> > > curl_easy_perform return success and works fine.
> > >
> > > But if set socket to non-blocking, the connect() return -1(rc = -1)
> > >
> > > and i got the following info: "errno in curl=0" and "errno out
> > > curl=150", why?
> > >
> > >
> > > /*../lib/connect.c*/
> > > /* set socket non-blocking */
> > > Curl_nonblock(sockfd, TRUE);
> > >
> > > rc = connect(sockfd, ai->ai_addr, ai->ai_addrlen);
> > >
> > > fprintf(stderr, "errno in curl=%d\n", errno);
> > >
> > > if(-1 == rc) {
> > > error = Curl_ourerrno();
> > > fprintf(stderr, "errno in curl=%d\n", errno);
> > >
> > >
> > > /*my multi-thread program:*/
> > > curl_easy_perform(curl);
> > > fprintf(stderr, "errno out curl=%d\n", errno);
> > >
> > --
> > Andy Hobbs <andy_at_hobbs.uk.net>
> >

-- 
Andy Hobbs <andy_at_hobbs.uk.net>
Received on 2004-10-25