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.这个名字数组直接能用




 
  
 


 




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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值