Re: How do I know ECONNREFUSED from non-blocking connect?

From: Steve (postmaster_at_127.0.0.1)
Date: 11/07/03


Date: Fri, 07 Nov 2003 22:08:23 GMT

On 7/11/03 7:39 pm, in article uc65hwdl2v.fsf@habanero.picante.com, "Grant
Taylor" <gtaylor+cup_cdgie110703@picante.com> wrote:

> Steve <postmaster@127.0.0.1> writes:
>
>> If I try a connect() on a non-blocking socket; how can I determine the error
>> ECONNREFUSED or ETIMEDOUT.
>>
>> Should I get the socket option SO_ERROR after my select() call tells me
>> something has happened?
>
> Yes, exactly.
>
> Note that, on Linux at least, you should not check the error value
> before the socket is reported as writable. Before the socket reports
> as writable, reading the error value gives no error, not even
> EINPROGRESS as you might expect. This is in fact indistiguishable
> from "happy connect", even when in fact the socket is not connected.

It'll be Mac OS X, actually.

> Similarly most any other socket operations will destroy the value, so
> don't go reading or writing if you really want to know what the
> connect result was.

OK.
 
> IMHO, this interface is slightly irritating; if your event reporter is
> in fact any sort of edge-triggered completion notification (such as
> pre-2.6 linux epoll, or most of the signal-based schemes) it is
> necessary to plug the socket fd into your interest set *between* the
> socket() and connect() calls - something many higher-level libraries
> make painful, since traditionally one does select or poll, where it
> doesn't matter.

I am actually writing a higher-level library! :)

The connect() is done in one thread, and the blocking select() is done in
another thread, so I can't guarantee that select() will be called before or
after connect().

Hmmm ... I hope the socket API is thread safe?

Steve.



Relevant Pages

  • Re: A Nice Fall Day....
    ... I love reading your reports. ... Writing about working with Miss M helps me work through it. ... Dang, though, I have to *watch* my body signals because she's ...
    (rec.equestrian)
  • Re: Suggestions for reading binary data from a connected socket.
    ... >to find an example explaining the best solution for reading and writing a ... >message from a socket. ...
    (comp.lang.java.programmer)
  • Re: Multithread safety
    ... What I meant to say is: If one thread is writing to ... the socket while another one is reading to it (full-duplex), ...
    (microsoft.public.win32.programmer.networks)
  • Re: Sharing the socket between 2 threads
    ... My plan is to use 2 threads: main for writing and slave for background ... reading. ... how to share the socket between 2 threads? ...
    (comp.lang.tcl)
  • Re: Multithread safety
    ... > the socket while another one is reading to it (full-duplex), ... > its impossible in certain cases for the writing thread to ...
    (microsoft.public.win32.programmer.networks)

Quantcast