NSNetService的address解析IP地址与端口
1 2 3 4 5 6 7 8 |
- (void)netServiceDidResolveAddress:(NSNetService *)sender{ NSLog(@"netServiceDidResolveAddress"); NSData *data = [sender TXTRecordData]; NSDictionary *info = [NSNetService dictionaryFromTXTRecordData:data]; NSDictionary *ipInfo = [self parsingIP:sender]; NSLog(@"parsingIP : %@", ipInfo); } |
#include <arpa/inet.h>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
- (NSDictionary *)parsingIP:(NSNetService *)sender{ int sPort = 0; NSString *ipv4; NSString *ipv6; for (NSData *address in [sender addresses]) { typedef union { struct sockaddr sa; struct sockaddr_in ipv4; struct sockaddr_in6 ipv6; } ip_socket_address; struct sockaddr *socketAddr = (struct sockaddr*)[address bytes]; if(socketAddr->sa_family == AF_INET) { sPort = ntohs(((struct sockaddr_in *)socketAddr)->sin_port); struct sockaddr_in* pV4Addr = (struct sockaddr_in*)socketAddr; int ipAddr = pV4Addr->sin_addr.s_addr; char str[INET_ADDRSTRLEN]; ipv4 = [NSString stringWithUTF8String:inet_ntop( AF_INET, &ipAddr, str, INET_ADDRSTRLEN )]; } else if(socketAddr->sa_family == AF_INET6) { sPort = ntohs(((struct sockaddr_in6 *)socketAddr)->sin6_port); struct sockaddr_in6* pV6Addr = (struct sockaddr_in6*)socketAddr; char str[INET6_ADDRSTRLEN]; ipv6 = [NSString stringWithUTF8String:inet_ntop( AF_INET6, &pV6Addr->sin6_addr, str, INET6_ADDRSTRLEN )]; } else { NSLog(@"Socket Family neither IPv4 or IPv6, can't handle..."); } } NSDictionary *data = @{@"type": [sender type], @"domain": [sender domain], @"name": [sender name], @"ipv4": ipv4, @"ipv6": ipv6, @"port": [NSNumber numberWithInt:sPort]}; return data; } - (void)parsingIP2:(NSNetService *)sender{ NSLog(@"%@",NSStringFromSelector(_cmd)); NSLog(@"----netService didResolveAddress %@ %@ %@ %@", sender.name, sender.addresses, sender.hostName, [sender.addresses firstObject]); NSArray *addresses = sender.addresses; [sender startMonitoring]; int port; NSString *ip; char addressBuffer[INET6_ADDRSTRLEN]; //服务的IP地址 for(NSData *data in addresses) { memset(addressBuffer, 0, INET6_ADDRSTRLEN); typedef union { struct sockaddr sa; struct sockaddr_in ipv4; struct sockaddr_in6 ipv6; } ip_socket_address; ip_socket_address *socketAddress = (ip_socket_address *)[data bytes]; if (socketAddress && (socketAddress->sa.sa_family == AF_INET || socketAddress->sa.sa_family == AF_INET6)) { const char *addressStr = inet_ntop( socketAddress->sa.sa_family, (socketAddress->sa.sa_family == AF_INET ? (void *)&(socketAddress->ipv4.sin_addr) : (void *)&(socketAddress->ipv6.sin6_addr)), addressBuffer, sizeof(addressBuffer)); int port = ntohs(socketAddress->sa.sa_family == AF_INET ? socketAddress->ipv4.sin_port : socketAddress->ipv6.sin6_port); if (addressStr && port) { port = port; ip = [NSString stringWithCString:addressStr encoding:NSASCIIStringEncoding]; NSLog(@"Found service at %s:%d", addressStr, port); } } } } |