initWithFileDescriptor:
Initializes and returns a file handle object associated with the specified file descriptor.
- (id)initWithFileDescriptor:(int)fileDescriptor
Parameters
- fileDescriptor
- The POSIX file descriptor with which to initialize the file handle. This descriptor represents an open file or socket that you created previously. For example, when creating a file handle for a socket, you would pass the value returned by the
socket
function.
Return Value of [NSFileHandle initWithFileDescriptor]
A file handle initialized with fileDescriptor.
Discussion of [NSFileHandle initWithFileDescriptor]
The file descriptor you pass in to this method is not owned by the file handle object. Therefore, you are responsible for closing the file descriptor at some point after disposing of the file handle object.
You can create a file handle for a socket by using the result of a
socket
call as fileDescriptor.
Example of [NSFileHandle initWithFileDescriptor]
-(void)createSocket
{
// create socket and wait for events to come in
NSSocketPort* serverSock = [[NSSocketPort alloc] initWithTCPPort: 1234];
socketHandle = [[NSFileHandle alloc] initWithFileDescriptor: [serverSock socket]
closeOnDealloc: NO];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(newConnection:)
name: NSFileHandleConnectionAcceptedNotification
object: socketHandle];
[socketHandle acceptConnectionInBackgroundAndNotify];
}