After the server-side Bonjour service is published successfully, the client can resolve the service through Nsnetservice, and after the successful resolution, the data details such as IP address, port and so on can be obtained.
The first step is to instantiate the Nsnetservice object code as follows:
- -(ID) init {
- _service = [[Nsnetservice alloc] initwithdomain:@ "Local."
- type:@ "_tonyipp._tcp" name:@ "Tony";
- [_service setdelegate:self];
- Set Resolve address timeout
- [_service Resolvewithtimeout:1.0];
- _services = [[Nsmutablearray alloc] init];
- return self;
- }
The constructor for instantiating a Nsnetservice object is –initwithdomain:type:name: You do not need to specify its port. The parsing service needs to invoke the [_service resolvewithtimeout:1.0] statement, begin parsing the service, parse it in the specified time, and the parameter is in seconds.
- #pragma mark-nsnetservicedelegate Methods
- -(void) Netservicewillresolve: (Nsnetservice *) Netservice {
- NSLog (@ "Netservicewillresolve");
- }
- -(void) netservicedidresolveaddress: (Nsnetservice *) Netservice {
- NSLog (@ "netservicedidresolveaddress");
- [_services Addobject:netservice];
- }
- -(void) Netservice: (Nsnetservice *) Netservice didnotresolve: (nsdictionary *) errordict {
- NSLog (@ "Didnotresolve:%@", errordict);
- }
Netservicewillresolve: Method callback at the beginning of parsing, callback netservicedidresolveaddress when parsing succeeds: method, callback Netservice:didnotresolve when parsing failed: method.
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://2009315319.blog.51cto.com/701759/1179965
Parsing Bonjour Service (RPM) in iOS