Re: Programming technique for non-forking servers?

From: <carson@dont-contact.us>
Date: Wed, 13 Nov 1996 15:28:39 -0500

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>
Received on Wed Nov 13 1996 - 12:30:05 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:33:32 MST