objective-c – 打开流后,NSNetServiceBrowser didRemoveService需要更长时间

前端之家收集整理的这篇文章主要介绍了objective-c – 打开流后,NSNetServiceBrowser didRemoveService需要更长时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码用于发现网络上的服务:
[netServiceBrowser setDelegate: self]; 
[netServiceBrowser searchForServicesOfType: serviceType inDomain: domain];

这导致调用这两种方法(查找服务和删除服务):

- (void) netServiceBrowser:(NSNetServiceBrowser*) netServiceBrowser 
         didFindService:(NSNetService*) netService ... {}

- (void) netServiceBrowser:(NSNetServiceBrowser*) netServiceBrowser 
         didRemoveService:(NSNetService*) netService ... {}

这很好用.当我关闭设备时,我立即收到didRemoveService调用.

但是,当我打开流(输入,输出或两者)到设备时:

[netService getInputStream: &inputStream outputStream: &outputStream];

[inputStream setDelegate: self];
[outputStream setDelegate: self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
             forMode: NSDefaultRunLoopMode];
[inputStream open];

[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
              forMode:NSDefaultRunLoopMode];
[outputStream open];

突然需要NSNetServiceBrowser几分钟才能检测到我关闭了设备
(调用didRemoveService需要一分钟).

我没有与之通信的设备(打开流)仍然会在我删除它们后立即调用didRemoveService.

更新:
这里有一些与我的问题有关的信息.

I’ve ran a trace with Wireshark and noticed the following:

I start my application in the iPad simulator,the application starts a
NSNetServiceBrowser and detects the printer. After that it opens the
input/output streams to the device (via airport express,usb). The
printer is sending me status updates and when I tap the test button in
my app the printer starts printing. In Wireshark I see all the
communication with the printer as expected.

Now when I start the exact same application on the iPad (and leave the
iPad simulator running). The application starts the
NSNetServiceBrowser as well,and detects the printer. The printer is
not sending me status updates and when I tap the test button,the
printer is not printing. In Wireshark I see the communication. The
printer or airport receives my commands and sends an ACK package.

As soon as I kill the iPad simulator app,the printer starts to print
the commands that I’ve sent using the iPad. It seems that opening a socket blocks all bonjour events,how can I prevent this from happening?

更多这里:https://devforums.apple.com/message/541436

解决方法

我确信它的In Lion不再为NSStreamEventEndEncountered事件调用处理程序.因此,当您确定已收到所有数据时,需要关闭输入流并将其从循环中删除.例如,当NSStreamEventHasBytesAvailable发生时.看看它,我想它应该工作
原文链接:https://www.f2er.com/c/118325.html

猜你在找的C&C++相关文章