O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

NSFileHandle

NSFileHandle provides methods that let you read and write data from a file or communication channel asynchronously in the background. Chapter 2 discussed file access with NSFileHandle. This section describes NSFileHandle’s asynchronous communications features and how they apply to networking.

You can obtain a socket file descriptor from an instance of NSSocketPort by sending a socket message to the socket port object. With the socket descriptor, you are able to initialize an instance of NSFileHandle with the method initWithFileDescriptor:. The initWithFileDescriptor:closeOnDealloc: method is an extension of this method that specifies whether or not the file descriptor will close when the file handle object is deallocated. By default, the file handle object does not close the file descriptor and ownership of that descriptor remains with the object that created the file handle. To determine an NSFileHandle instance’s file descriptor, invoke the method fileDescriptor.

NSFileHandle provides the following three methods for performing asynchronous background communication:

acceptConnectionInBackgroundAndNotify

This method is valid only for NSFileHandle instances initialized with a socket file descriptor (of type SOCK_STREAM), and causes the socket represented by the file handle to listen for new connections. This method returns immediately while a background thread accepts client connections over the socket. Observers are notified of new connections by registering for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required