iOS and OS X Network Programming Cookbook |Performing a network address resolution

11 篇文章 0 订阅

这个部分解决的是host/service name 和 sockadd structure相互转换的问题。

用到的两个方法:

Getaddrinfo():通过给定的host/service name返回相关信息,存储在addrinfo structure中.

Getnameinfo():使用给定的sockaddr structure得到host/service name。


主要步骤(BSD版本):

1.创建一个AddrInfo类,继承自NSObject

#import <netdb.h>
#import <netinet/in.h>
#import <netinet6/in6.h>

@property (nonatomic, strong) NSString *hostname, *service;
@property (nonatomic) struct addrinfo *results;
@property (nonatomic) struct sockaddr *sa;
@property (nonatomic, readonly) int errorCode;
    -(void)addrWithHostname:(NSString*)lHostname Service:(NSString *)
   lService andHints:(struct addrinfo*)lHints;
    -(void)nameWithSockaddr:(struct sockaddr *)saddr;
    -(NSString *)errorString;

这其中定义的第一个方法将会使用getaddrinfo()方法得到results,第二个方法使用getnameinfo()得到hostname和serviece。

2.实现AddInfo类

 -(void)setVars {
       self.hostname = @"";
       self.service = @"";
       self.results = @"";
       _errorCode = 0;
}

 -(void)addrWithHostname:(NSString*)lHostname Service:(NSString *)
   lService andHints:(struct addrinfo*)lHints {
       [self setVars];
       self.hostname = lHostname;
       self.service = lService;
       struct addrinfo *res;
       _errorCode = getaddrinfo([_hostname UTF8String], [_service
   UTF8String], lHints, &res);
       self.results = res;
   }

getaddrinfo()中需要的hostname和service的格式是char array格式,所以要进行转换。如果成功的话errorcode = 0


-(void)nameWithSockaddr:(struct sockaddr *)saddr {
       [self setVars];
       char host[1024];
       char serv[20];
       _errorCode = getnameinfo(saddr, sizeof saddr, host, sizeof host,
       serv, sizeof serv, 0);
       self.hostname = [NSString stringWithUTF8String:host];
       self.service = [NSString stringWithUTF8String:serv];
}

代码非常简洁明了,差不多就是上面代码的逆向,成功返回0,不成功返回-1

-(void)setResults:(struct addrinfo *)lResults {
    freeaddrinfo(self.results);
    _results = lResults;
}

在results设置新值前释放旧值


3.main函数中的调用

</pre><pre name="code" class="objc">struct addrinfo *res;
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

AddrInfo *ai = [[AddrInfo alloc] init];
   [ai addrWithHostname:@"www.packtpub.com" Service:@"443"
     andHints:&hints];
struct addrinfo *results = ai.results;
     for (res = results; res!= NULL; res = res->ai_next) {
         void *addr;
         NSString *ipver = @"";
         char ipstr[INET6_ADDRSTRLEN];

if (res->ai_family == AF_INET) {
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;

    addr = &(ipv4->sin_addr);
    ipver = @"IPv4";
} else if (res->ai_family == AF_INET6){
    struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_<pre name="code" class="objc">addr;
    addr = &(ipv6->sin6_addr);
    ipver = @"IPv6";
} else {
    continue; }

         inet_ntop(res->ai_family, addr, ipstr,sizeof ipstr);
         NSLog(@"     %@  %s", ipver, ipstr);
         AddrInfo *ai2 = [[AddrInfo alloc] init];
         [ai2 getNameWithSockaddr:res->ai_addr];
         if (ai2.errorCode ==0)
             NSLog(@"--%@ %@",ai2.hostname, ai2.service);
     }
     freeaddrinfo(results);


CFNetwork的方法感觉更加复杂,真是找虐啊

主要步骤(CFNetwork部分)

Hostname转换为IP地址
1.使用给定的hostname,利用CFHostCresteWithName创建CFHostRef
2.使用CFHostStartInfoResolution对hostRef进行分解
3.使用CFHostGetAddress取得地址(返回值是数组CFArrayRef),CFArrayGetCount取得地址数组的个数
4.然后使用CFDataGetBytePtr将地址数组中每个数返回为sockaddr struct,再利用getnameinfo()得到ipaddr

Address返回Hostname
1.给定的address,使用getaddrinfo返回addrinfo struct --results
2.使用results中的ai_addr 和 ai_addrlen, 以及CFDataCreate返回CFDataRef
3.CFHostCresteWithAddress创建CFHostRef
4.分解
5.CFHostNames取得名字
6.这个名字数组直接能用




 
  
 


 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In Detail Darwin, which forms the core set of components for OS X and iOS, is compatible with the Single UNIX Specification Version 3 and POSIX UNIX. Therefore OS X and iOS are UNIX operating systems. While there are numerous books and articles written to teach network development in a UNIX environment there is surprisingly little dedicated to teaching network development specifically in an Apple environment. In this book we will be covering the standard UNIX networking stack with BSD Sockets as well as Apple’s frameworks like CFNetworking and Bonjour. "iOS and OS X Network Programming Cookbook" is an enthralling code-focused book on developing client and server applications for the iOS and OS X environments. You will learn how to integrate a number of Unix (BSD Sockets), Apple specific (CFNetworking, NSMutableURLRequest, Bonjour) and third party APIs (AFNetworking, MKNetworking) with your own apps. "iOS and OS X Network Programming Cookbook" begins by showing you how to build both client and server applications using the BSD Socket Library as well as Apple’s CFNetworking library. The sample code includes samples for both iOS and OS X. It then covers how to add Bonjour to publish, find, and connect to services. This can be used to create peer-to-peer applications over local networks. Finally it shows how to integrate third-party libraries like Libnet, LibPCAP, AFNetworking, and MKNetworking with the reader’s apps. A lot of developers do not realize that iOS is a full UNIX-based operating system that contains a full set of networking APIs. These APIs can be used to create both client- and server-based applications in the iOS environment as well as OS X. Using the Apple’s Bonjour library you will learn how to publish, find, and connect the services together without hardcoding the connection information. There are also a number of third-party APIs that can be used to easily connect iOS and OS X applications to web-based services. Finally this book will cover several low level libraries like LibNET and LibPCAP on the OS X environment. "iOS and OS X Network Programming Cookbook" will cover several UNIX, Apple, and third-party libraries and APIs. The book will show the user how to integrate all of these libraries and APIs with their applications. Approach This book follows a recipe-based approach that will heavily focus on the code and how to integrate the samples with the reader’s projects.Each recipe consists of one or more methods that you can put directly into your app and use. Who this book is for This book is ideal for developers that want to create network applications for the Apple OS X or iOS platforms. All examples are written in Objective-C using XCode as the IDE. Knowledge of Objective-C and X-Code is essential.
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值