IOS network programming-Parse Bonjour service in iOS

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.