NSNetServiceBrowser didRemoveService takes longer after opening a stream

I have the following code to find services on the network:

[netServiceBrowser setDelegate: self]; [netServiceBrowser searchForServicesOfType: serviceType inDomain: domain]; 

This leads to calls to these two methods (find the service and remove the service):

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

It works great. When I disconnect my device, I immediately receive a didRemoveService call.

However, when I open a stream (input, output, or both) to the device:

 [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]; 

It suddenly takes NSNetServiceBrowser for almost a minute to discover that I turned off the device
(it took a minute to call didRemoveService).

Devices that I don’t communicate with (opening threads with) still call didRemoveService as soon as I delete them.

Update: Here is a bit more information related to my problem.

I traced with Wireshark and noticed the following:

I run my application in the iPad simulator, the application launches NSNetServiceBrowser and it detects the printer. After that, it opens the input / output streams to the device (via airport express, USB). the printer sends me status updates and when I click the test button in my application it starts printing. In Vireshark, I see all communication with the printer as expected.

Now when I launch the same application on the iPad (and leave the iPad simulator working). The application launches NSNetServiceBrowser and also detects the printer. The printer is not sending me status updates, and when I click the test button, the printer does not print. In Vireshark I see a message. the printer or airport receives my commands and sends the ACK packet.

As soon as I kill the iPad simulator application, the printer will start printing the commands that I sent using the iPad. It seems that opening a socket blocks all bonjour events, how can I prevent this?

More details here: https://devforums.apple.com/message/541436

+7
source share
2 answers

This seems to be a limitation of Express Airport.

My current implementation with GCDAsyncSocket works very well, and I just have to make sure that only one socket is used to communicate with Express Express.

I close the question.

0
source

I am sure that in Leo the handler has not yet raised the NSStreamEventEndEncountered event. Thus, you need to close the input stream and remove it from the loop when you are sure that you have received all the data. For example, when NSStreamEventHasBytesAvailable occurs. Check it out and I think it should work

0
source

All Articles