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 ...
随机推荐
- php比较两个数组的差异array_diff()函数
下面简单介绍php比较两个数组的差异array_diff()函数. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.php/archives/ ...
- Linux的基础命令大全
linux的基础命令大全 1.shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口(命令解释器) ls -al /bin/sh ls -al /bin/bash 查看这些shell的 ...
- python3 练习题100例 (三)
题目三:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? #!/usr/bin/env python3 # -*- coding: utf-8 -*- &qu ...
- kali aquatone安装
https://www.jianshu.com/p/418eedb9d9c8
- Java——多线程---18.11.22
多线程代码:Runnable方法 package com.hebust.java.third; import java.util.Random; public class SaleTicket imp ...
- Android APP架构设计——MVP的使用示例
0. 前言 为了更好地进行移动端架构设计,我们最常用的就是MVC.MVP和MVVM,作为三个最耳熟能详的三大架构,应用可谓非常广泛.对于这三种架构设计以及优缺点已经在Android APP架构设计-- ...
- 成都Uber优步司机奖励政策(1月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- MySql慢查询日志——开启/查看/删除
1,开启慢查询日志 修改mysql.ini文件,加入如下配置: [mysqld] log-slow-queries=H:\mysql_log\slow_query.log long-query-tim ...
- CakePHP Model中( 获取Session)使用Component的方法
有时候我们需要在Model中使用Session,大家知道CakePHP把操作Session的方法封装为了一个Component, 在Model中正常读取Session的方法: 在 "app_ ...
- pg 与 oracle 比较
所谓动态引擎,就是说比如有很多张表的Join,原始的做法是一开始就生成好这个执行计划,随后执行,但实际上很多表Join的时候,你一开始生成的那个执行计划很有可能是不对的. 那么动态执行计划就是指它可以 ...