iOS- Apple零配置网络协议Bonjour的使用?
1.前言
2.什么是Bonjour?能做些什么?
2.1 IP获取
2.2 名称解析
2.3 服务发现
3.Bonjour的使用?
3.1.Bonjour服务端注册服务
//首先我们需要使用到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;
3.2.Bonjour客户端发现本地服务
//客户端主要使用的是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的使用?的更多相关文章
- 网络协议之mDNS20170217
DNS(Domain Name System,域名系统)因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终得 ...
- 网络协议之mDNS
DNS(Domain Name System,域名系统)因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终得 ...
- 快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求
本文快速分享一下快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求的方法,供大家参考. 原文发表于我的技术博客 零配置方案 最新的苹果审核政策对 API 的 IPv6 以及 ...
- iOS网络协议 HTTP/TCP/IP浅析
一.TCP/IP协议 话说两台电脑要通讯就必须遵守共同的规则,就好比两个人要沟通就必须使用共同的语言一样.一个只懂英语的人,和一个只懂中文的人由于没有共同的语言(规则)就没办法沟通.两台电 ...
- iOS直播-播放基于RTMP协议的视频
iOS直播-播放基于RTMP协议的视频 流媒体协议介绍 1. 伪流媒体: 渐进式下载 : 边下边存, 文件会保存 使用http协议,也能够实现视频播放, 也能快进快退等, 体验上跟流媒体很像. 优酷, ...
- iOS开发人员程序许可协议
请细致阅读以下的许可协议条款和条件之前下载或使用苹果软件. 这些条款和条件构成你和苹果之间的法律协议. iOS开发人员程序许可协议 目的 你想使用苹果软件(例如以下定义)来开发一个或多个应 ...
- 011-通过网络协议解析网络请求-DNS-ARP-TCPIP
一.概述 1.1.tcp/ip概述 TCP/IP不是一个协议,而是一个协议族的统称.里面包括IP协议.IMCP协议.TCP协议.跨越了多层模型的多层 TCP/IP协议族按照层次由上到下,层层包装.最上 ...
- EPF:一种基于进化、协议感知和覆盖率引导的网络协议模糊测试框架
本文系原创,转载请说明出处:from 信安科研人 目录 实验 工具的安装 1.安装AFL++ 2.安装epf 对IEC104协议库进行fuzz 实验准备 使用AFL++中的编译器插桩 开始fuzz 原 ...
- FREEBSD手工配置网络
在FreeBSD系统中,网络能力十分重要,对于一个标准的FreeBSD系统,至少要有一个网络界面以便与其他计算机通信.最常见的网络界面为以太网卡.此外FreeBSD也支持Token Ring和FDDI ...
随机推荐
- 使用Screen管理远程会话
在本地开发时,经常需要使用远程连接到Linux服务器,一开始我自己都是有几个远程就开几个SSH窗口,这种方法很原始很直接,但每次都受够了密码输入,即使用了SSH免密码登录,也会觉得每次输入SSH的 ...
- Linux的基础命令大全
linux的基础命令大全 1.shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口(命令解释器) ls -al /bin/sh ls -al /bin/bash 查看这些shell的 ...
- java 关键字super和this
super关键字 作用:调用父类的构造器 只能出现在子类的构造其中,并且必须是第一行 super()中的参数,决定了调用父类的那个构造器 注:如果子类构造器中没有出现super,则默认加上super( ...
- 20145207 myeclipse测试
实验博客
- 20145209 2016-2017-2 《Java程序设计》第10周学习总结
20145209 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 计算机网络概述 计算机网络体系结构的通信协议划分为七层,自下而上依次为:物理层(Physi ...
- Caliburn.Micro 杰的入门教程6, Screens 和 Conductors 简介
Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...
- MapWinGIS介绍
MapWinGIS是一个不错的开源组件GIS项目,基于微软的COM思想编写,团队的开发人员主要来自爱荷华大学,而且开发团队中还有一位中国人.它的功能类似于AO(当然没有AO那么强大),其核心是一个名字 ...
- SpringBoot-02:SpringBoot中的POM文件详细解释
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 我把pom文件,以及它的详细解释发出来 <?xml version="1.0" en ...
- dubbo之监控中心(monitor)
一.monitor是dubbo框架中的一个监控中心.这个只是针对于消费者和提供者进行一个数据记录,不参与业务和使用.当然当monitor挂掉之后,也不会影响服务的正常运行. 二.在阿里的dubbo中也 ...
- WeTest功能优化第3期:业内首创,有声音的云真机
第3期功能优化目录 [云真机远程调试]音频同步传输实现测试有声 [兼容性测试报告]新增视频助力动态定位问题 [云真机远程调试]菜单栏优化助力机型选择 本期介绍的新功能,秉承创造用户需求的理念,在云真机 ...