使用 NSFileHandle 发送大量数据
因此,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我基本上错过了一个步骤......在 NSNetServiceBrowser 的 netServiceBrowser: didFindService:( moreComing: 委托方法中,我不是简单地尝试连接到每个传入服务,而是(正如文档所说:))保留服务,为找到的服务设置委托,并尝试解析该服务。
然后,我可以在 *- (void)netServiceDidResolveAddress:(NSNetService )sender 中打开已解析服务的流,这是 NSNetservice 的委托方法。
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.