[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?