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发生时.看看它,我想它应该工作

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...