使用 NSFileHandle 发送大量数据

发布于 2024-09-07 23:44:32 字数 652 浏览 6 评论 0原文

因此,我使用 Apple 的 PictureSharing/PictureSharingBrowser 示例来发送和接收数据。这在服务器端使用 NSFileHandle 来使用 NSFileHandle 的 writeData 方法发送图片。

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

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

这似乎工作正常,直到我想要发送大量数据(在本例中为 1MB 的数据)。当我尝试执行此操作时,应用程序在执行 writeData 方法时挂起。客户端甚至没有开始读取数据,它只是打开连接,但什么也没有发生。 (它应该逐块读取数据,而服务器一次发送所有数据)。

我猜测某个地方发生了一些僵局,但我不确定在哪里。我试图寻找异步。通过chuck用NSFileHandle写入数据chuck的方法,但我找不到这样的方法。

任何指导都会有所帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-09-14 23:44:32

我基本上错过了一个步骤......在 NSNetServiceBrowser 的 netServiceBrowser: didFindService:( moreComing: 委托方法中,我不是简单地尝试连接到每个传入服务,而是(正如文档所说:))保留服务,为找到的服务设置委托,并尝试解析该服务。

然后,我可以在 *- (void)netServiceDidResolveAddress:(NSNetService )sender 中打开已解析服务的流,这是 NSNetservice 的委托方法。

- (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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文