mDNS(组播DNS)

参考:
什么是Bonjour
实现零配置网络联网的解决方案
cordova-plugin-zeroconf

终端注册service:
dns-sd -R "test" _spider._tcp . 9000 i=love

完整代码示例:

#import "CDVMDNS.h"
#include 

@interface CDVMDNS ()

@property (strong,nonatomic) NSNetService *netService;

@property (strong,nonatomic)NSNetServiceBrowser *browser;
@property NSMutableArray *services;
@property (strong,nonatomic) NSNetService *service;

@end


@implementation CDVMDNS

-(void)pluginInitialize{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appSetupPluginDidFinishLaunchingNotification:)
                                                 name:UIApplicationDidFinishLaunchingNotification
                                               object:nil];
}


-(void)appSetupPluginDidFinishLaunchingNotification:(NSNotification *)notification{
    
    NSLog(@"appSetupPluginDidFinishLaunchingNotification!");
    
    self.netService = [[NSNetService alloc]initWithDomain:@"local." type:@"_spider._tcp." name:@"" port:2333];
    self.netService.delegate = self;
    [self.netService publish];
    
    [self performSelector:@selector(getSevices:) withObject:nil afterDelay:10];
    
}


-(void)getSevices:(CDVInvokedUrlCommand*)command{
    
    if (self.browser) {
        [self.browser stop];
    }
    else{
        self.browser = [[NSNetServiceBrowser alloc]init];
        self.browser.delegate = self;
    }
    [self.browser searchForServicesOfType:@"_spider._tcp." inDomain:@"local."];
}



- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser     didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
    
    [self.services addObject:aNetService];
    
    [self performSelector:@selector(setService:) withObject:aNetService afterDelay:10];
    
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
    [self.services removeObject:aNetService];
    
}

- (void)setService:(NSNetService *)service
{
    _service = service;
    self.service.delegate = self;
    [self.service resolveWithTimeout:5];
}

- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
    //get service's hostName
    NSString *host = sender.hostName;
    //get address data
    NSData *address = sender.addresses.firstObject;
    
    NSString *ip = [self IPFromData:address];
//    NSData* data = [sender TXTRecordData];
//    NSDictionary* dict = [NSNetService dictionaryFromTXTRecordData:data];
//    NSData *str = [dict objectForKey:@"MAC"];
//    NSString *oldmacadress = [[NSString alloc]initWithData:str encoding:NSUTF8StringEncoding];
//    NSString *macadress = [oldmacadress stringByReplacingOccurrencesOfString:@":" withString:@""];
    
}
-(NSString*)IPFromData:(NSData*)data
{
    
    char addressBuffer[INET6_ADDRSTRLEN];
    
    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)
        {
            NSLog(@"Found service at %s:%d", addressStr, port);
            return [NSString stringWithFormat:@"%s:%d",addressStr,port];
        }
    }
    return @"";
}
@end

ps:
此处用终端生成service是因为在客户端创建的service总是会被删掉,需要不停的调用,在客户端生成的还没成功获得过,原因未知。

你可能感兴趣的:(mDNS(组播DNS))