Question

So I'm using Apple's PictureSharing/PictureSharingBrowser samples to send and receive data. This uses an NSFileHandle on the server side to send a picture using NSFileHandle's writeData method.

NSFileHandle * incomingConnection = [[aNotification userInfo] objectForKey:NSFileHandleNotificationFileHandleItem];

[[aNotification object] acceptConnectionInBackgroundAndNotify];
[incomingConnection writeData:dataToWrite];
[incomingConnection closeFile];

This seems to work fine until I want to send large amounts of data (in this case 1MB worth of data). When I attempt this, the application hangs while executing the writeData method. The client doesn't even begin reading the data, it simply opens the connection, but nothing happens. (it's supposed to read the data chunk by chunk, while the server sends all teh data at once).

I'm guessing some deadlock is occurring somewhere, but i'm not sure where. I tried to look for an async. way of writing the data chuck by chuck with NSFileHandle, but i could not find such a way.

Any guidance would help!

Was it helpful?

Solution

I missed one step basically... in NSNetServiceBrowser's netServiceBrowser: didFindService:( moreComing: delegate method, instead of me simply trying to connect to every incoming service, I instead (as the doc says :) ) retain the service, set the delegate for that found service, and attempt to resolve the service.

I am then able to open a stream to the resolved service in *- (void)netServiceDidResolveAddress:(NSNetService )sender which is NSNetservice's delegate method.

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    [aNetService retain];
    [aNetService setDelegate:self];
    [aNetService resolveWithTimeout:5.0];

}

- (void)netServiceDidResolveAddress:(NSNetService *)service{

    NSInputStream * istream;
    [sender getInputStream:&istream outputStream:nil];
    [istream retain];
    [istream setDelegate:self];
    [istream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [istream open];

    [service release];
}

//... NSStreamDelegate method to retrieve the data via the stream.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top