views:

174

answers:

1

I have a BSD socket created with following code (it's in external library that I cannot change):

fcntl(sock, F_SETFL, O_NONBLOCK);
connect(sock, (struct sockaddr*) &sin, sizeof(sin))

What I can do to get a notification from Cocoa that connection is established? In regular world I would make select(3) and test for writability but this either blocks or requires polling (or I need a thread)

I tried with NSFileFandle but this only allows me to test if there are new data available not that connection is ready for writing.

+1  A: 

I also fail to see such a mechanism in the Cocoa examples and docs.

The thing is, though, I also don't see a kernel mechanism that could be used to create such a mechanism in Cocoa. Without kernel support, even if there is a way to get an asynchronous connection notification from some high-level framework, it's not going to be more efficient than what you propose, calling select() in a thread.

You could refine the idea somewhat, such as by using kqueue() instead of select(), and pooling all the app's file handles and socket descriptors together so you don't need a thread per.

Warren Young