After the Bonjour service on the server is successfully published, the client can use NSNetService to parse the service. After successful resolution, the client can obtain the details of the communication data, such as the IP address and port.
First, you need to instantiate the NSNetService Object Code as follows:
[Cpp]
-(Id) init {
_ Service = [[NSNetService alloc] initWithDomain: @ "local ."
Type: @ "_ tonyipp. _ tcp." name: @ "tony"];
[_ Service setDelegate: self];
// Set resolution address timeout
[_ Service resolveWithTimeout: 1.0];
_ Services = [[NSMutableArray alloc] init];
Return self;
}
-(Id) init {
_ Service = [[NSNetService alloc] initWithDomain: @ "local ."
Type: @ "_ tonyipp. _ tcp." name: @ "tony"];
[_ Service setDelegate: self];
// Set resolution address timeout
[_ Service resolveWithTimeout: 1.0];
_ Services = [[NSMutableArray alloc] init];
Return self;
}
The construction method for instantiating the NSNetService object is-initWithDomain: type: name:. You do not need to specify its port. The Resolution service must call the [_ service resolveWithTimeout: 1.0] statement to start parsing the service. The parameter is in seconds.
[Cpp]
# 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 );
}
# 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: The method is called back at the resolution start, and netServiceDidResolveAddress: method when the resolution is successful. netService: didNotResolve: method is called back when the resolution fails.