Software Engineering (Mobile Technologies)
Sunag is currently working in Mercedes-Benz Research & Development India for the New Technolo... more>>
Imagine a scenario where in you go with your laptop to your client's place and want to use a printer there. How would it be if you just connect your laptop to the local network and your laptop shows you the list of available printers? Say you want to play a network game with your friends. As soon as you connect to the network and open the game you see your friends name on the screen. All this happening without you having to know the IP address and the port number of the host/service you wish to connect to!! All this happening without centralized allocation of network addresses, without name server and without a centralized repository of available services. This simple and reliable way to configure and browse for services and choose one from the list instead of having to know each service's name or IP address in advance is exactly the capability that Bonjour provides.
The Bonjour technique has to accomplish 3 fundamental operations
Consider a hypothetical device or a host that can share music over the local network. Let the underlying transport protocol be UDP. Let the name of the application protocol or the service type be 'music' and instance name of the service be 'Hindustani-classical-music'. Any host that queries for a service named 'music' gets back 'Hindustani-classical-music' as one of the instances (there may be similar devices in the same network under the service type 'music' but with different instance name)
Once this device is plugged in to the network a series of operations take place.
Now consider a client application browsing for music sharing services. The client issues a query for service of type _music_udp in the local domain. Every Multicast DNS responder on the network hears this request, but only the music sharing device responds.
The resolution begins with a DNS query asking for the Hindustani-classical-music._music._udp.local SRV record. This query returns the host's name Hindustani-classical-music.local., and port number 1125. The client now sends out a multicast request for the IP address. This request resolves to the IP address 169.254.170.30. The client can then use the IP address and the port number to connect to the service.
Publishing a service
To publish a service on the network, NSNetService class is used.
The presence can be advertised on the network by publishing a network service when the application has finished launching (application:DidFinishLaunchingWithOptions:).
NSNetService class: netService = [[NSNetService alloc] initWithDomain:@""
The first argument specifies the domain for the service. @"" is used to denote the default domain. The second argument indicates the service type and transport layer. In this example, the name of the service is Music and it uses UDP as the protocol. The service name and the protocol has to be prefixed with an underscore(_) and the protocol ends with a period(.) The third argument specifies the name of the service. Finally, the port number on which the service is published via the fourth argument.
To publish the service, publish method of the NSNetService class has to be used:
netService.delegate = self;
netService:didNotPublish: method is implemented so that in the event that the service is not published successfully, appropriate alert can be displayed to the user.
When the application exits (applicationWillTerminate:) or goes into background mode
(application DidEnterBackground:) publishing the service has to be stopped:
Browsing for Services
An instance of NSNetServiceBrowser class is created and used to search for the services.
browser = [[NSNetServiceBrowser new] autorelease];
As services are discovered, the netServiceBrowser:didFindService:moreComing: method will be called. In this method, the discovered services can be added to a mutable array :
The IP address of the discovered service can be resolved by calling the resolveIPhonedress: method. The resolveIPAddress: method uses the resolveWithTimeout: method of the NSNetService instance (representing the service that was discovered) to obtain its IP addresses:
If it managed to resolve the IP addresses of the service, the netServiceDidResolveAddress: method is called. If it did not manage to resolve the IP address, the netService:didNotResolve: method is called
When services are removed from the network, the netServiceBrowser:didRemoveService: method is called. Therefore, in this method the services from the mutable array can be removed.
Experts on Mobile
Chief Tech Architect
Sr Software engineer