标签:des style blog http io color ar os 使用
//首先我们需要使用到iOS SDK里的NSNetService类 @property(strong,nonatomic)NSNetService*netService;
//初始化服务,指定服务的域,类型,名称和端口 _netService = [[NSNetService alloc] initWithDomain:@"local." type:@"_http._tcp." name:@"DamonWebServer" port:5222];
//指定代理 [_netServicesetDelegate:self]; //发布注册服务 [_netService publish];
//NSNetServiceDelegate代理协议相关代理方法 按需求使用 @protocolNSNetServiceBrowserDelegate <NSObject> @optional /* Sent to the NSNetServiceBrowser instance‘s delegate before the instance begins a search. The delegate will not receive this message if the instance is unable to begin a search. Instead, the delegate will receive the -netServiceBrowser:didNotSearch: message. */ - (void)netServiceBrowserWillSearch:(NSNetServiceBrowser*)aNetServiceBrowser; /* Sent to the NSNetServiceBrowser instance‘s delegate when the instance‘s previous running search request has stopped. */ - (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser*)aNetServiceBrowser; /* Sent to the NSNetServiceBrowser instance‘s delegate when an error in searching for domains or services has occurred. The error dictionary will contain two key/value pairs representing the error domain and code (see the NSNetServicesError enumeration above for error code constants). It is possible for an error to occur after a search has been started successfully. */ - (void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowser didNotSearch:(NSDictionary*)errorDict; /* Sent to the NSNetServiceBrowser instance‘s delegate for each domain discovered. If there are more domains, moreComing will be YES. If for some reason handling discovered domains requires significant processing, accumulating domains until moreComing is NO and then doing the processing in bulk fashion may be desirable. */ - (void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowser didFindDomain:(NSString*)domainString moreComing:(BOOL)moreComing; /* Sent to the NSNetServiceBrowser instance‘s delegate for each service discovered. If there are more services, moreComing will be YES. If for some reason handling discovered services requires significant processing, accumulating services until moreComing is NO and then doing the processing in bulk fashion may be desirable. */ - (void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowser didFindService:(NSNetService*)aNetService moreComing:(BOOL)moreComing; /* Sent to the NSNetServiceBrowser instance‘s delegate when a previously discovered domain is no longer available. */ - (void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowser didRemoveDomain:(NSString*)domainString moreComing:(BOOL)moreComing; /* Sent to the NSNetServiceBrowser instance‘s delegate when a previously discovered service is no longer published. */ - (void)netServiceBrowser:(NSNetServiceBrowser*)aNetServiceBrowser didRemoveService:(NSNetService*)aNetService moreComing:(BOOL)moreComing;
//客户端主要使用的是iOS SDK里的NSNetServiceBrowser @property(strong,nonatomic)NSNetServiceBrowser*serverBrowser; @property(strong,nonatomic)NSMutableArray*servers; //NSNetService在客户端用于解析 @property(strong,nonatomic)NSNetService*netserver; //初始化NSNetServiceBrowser _serverBrowser= [[NSNetServiceBrowseralloc]init]; //指定代理 _serverBrowser.delegate = self; _servers = [NSMutableArrayarray]; //查找服务 接着使用NSNetServiceBrowser实例的searchForServicesOfType方法查找服务,方法中可以指定需要查找的服务类型和查找的域 [_serverBrowsersearchForServicesOfType:@"_http._tcp."inDomain:@"local."];
NSNetServiceBrowserDelegate代理协议相关代理方法
//即将解析服务, - (void)netServiceWillResolve:(NSNetService *)netService { NSLog(@"netServiceWillResolve"); } //解析服务成功 - (void)netServiceDidResolveAddress:(NSNetService *)netService { NSLog(@"service ip:%@,------port:%d",netService.addresses,netService.port); } //解析服务失败,解析出错 - (void)netService:(NSNetService *)netService didNotResolve:(NSDictionary *)errorDict { NSLog(@"didNotResolve: %@",errorDict); } //已发现服务 - (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing { NSLog(@"didFindService"); _netserver = netService; _netserver.delegate = self; //设置解析超时时间 [_netserverresolveWithTimeout:5.0]; }
iOS- 如何使用Apple的零配置网络协议Bonjour?
标签:des style blog http io color ar os 使用
原文地址:http://www.cnblogs.com/qingche/p/4087771.html