Unix networking uses file descriptors for reading and writing. Using file descriptors in server programs is pretty easy with the select() and kevent() functions. Using sockets in end-user GUI apps presents a problem, though. You cannot call select() on the main thread to wait for network activity. This will cause the UI to hang until there is something to be done on the network. Blocking the UI thread would cause your program to become unresponsive, and your process would be killed on iOS devices.

There are some options available to you. You can put your blocking function into a new thread. That thread can then block to its heart’s content until there is activity on the network, and then it can inform the UI thread that there is ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.