views:

507

answers:

1

I'm connecting a socket asynchronously (O_NONBLOCK + connect). POSIX standard specifies that after socket has been connected is should signal the event by making the file descriptor for the socket ready for writing. It doesn't seem to say anything about failures during async connect.

When testing it on Linux, it seems that sometimes I'm getting POLLOUT and sometimes POLLERR in this situation. Is there any pattern in the behaviour? Can I make it report the errors in a single way? Does POSIX say something I have overlooked?

+3  A: 

D. J. Bernstein has some stuff that looks like it might be relevant: http://cr.yp.to/docs/connect.html. In particular he suggests several different ways to get errors out.

The UNIX Socket FAQ has a section on Connect with timeout, that includes getting the error from a failed connection using getsockopt

Douglas Leeder
Both texts assume that the connect notifies the user about connection success/failure by POLLOUT event. However, what I am seeing is that Linux uses either POLLOUT or POLLERR for this purpose.