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 typeSOCK_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 ...
Get Cocoa in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.