Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Nov 1996 15:28:39 -0500
From:      carson@lehman.com
To:        Terry Lambert <terry@lambert.org>
Cc:        stesin@gu.net (Andrew Stesin), hackers@freebsd.org, squid-users@nlanr.net, basch@lehman.com
Subject:   Re: Programming technique for non-forking servers?
Message-ID:  <199611132028.PAA18081@dragon.lehman.com>
In-Reply-To: <199611131938.MAA22676@phaeton.artisoft.com>
References:  <Pine.BSI.3.95.961113122121.8648B-100000@creator.gu.kiev.ua> <199611131938.MAA22676@phaeton.artisoft.com>

next in thread | previous in thread | raw e-mail | index | archive | help

Unfortunately, your code fragment doesn't handle async connect
properly. Error handling for async connect is non-trivial to write
portably. Here are the possible statuses and select() returns:

Connect Status	Select Return
--------------	-------------
inprogress	nothing
success		write _and_, possibly, read (if data was written from the
		far side before select returned)
failure		read (and usually write as well, but some OS's seem to be
		broken)

So, the question is, how do you tell if connect actually failed, and what
the errno was? Good question... Here are your options:

1) getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&err, &len)

Nice, but some systems (HP?) don't support SO_ERROR <sigh>

2) read(fd, buf, 0)

Will either succeed or fail and set errno to the connect failure
cause. Unless the system doesn't support 0-byte reads... or it doesn't
return connect's error in errno... <sigh>

3) getpeername()

Works everywhere, but looses the connect error...

Oh, and don't forget that sometimes connect() will return immediately, even
if the socket is non-blocking, so you have to check for all possible states
(success, non-blocking, or error). And be carefull, connect() can return
different values for non-blocking on different OS's (EINPROGRESS,
EWOULDBLOCK, etc.)

--
Carson Gaspar -- carson@cs.columbia.edu carson@lehman.com
http://www.cs.columbia.edu/~carson/home.html
<This is the boring business .sig - no outre sayings here>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199611132028.PAA18081>