문제

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!

도움이 되었습니까?

해결책

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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top