简体   繁体   中英

NSNetService Problems

I am trying to establish a Bonjour connection between an iOS device and a Mac. Discovering each other is working great, but I am having problems with setTXTRecordData: . It always fails (BOOL returns NO)...

_serviceInstances creation:

-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
    [aNetService retain];

    [aNetService setDelegate:self];
    [aNetService startMonitoring];

    [aNetService performSelectorOnMainThread:@selector(resolve) withObject:nil waitUntilDone:YES];
[_serviceInstances addObject:aNetService];
}

Sending attempt:

NSNetService*service = [_serviceInstances objectAtIndex:[servicesTable selectedRow]];

[service setDelegate:self];

NSDictionary*txtRecordDataDictionary = [NSDictionary dictionaryWithObject:@"2" forKey:@"Version"];

if (service)
{
    BOOL success = [service setTXTRecordData:[NSNetService dataFromTXTRecordDictionary:txtRecordDataDictionary]];   

    if (!success)
    {
        NSRunCriticalAlertPanel(@"Sync Error", @"Failed to contact Client. Please restart Carbon on your iPad and try again.", @"OK", nil, nil);
    }

    NSLog(@"Service: %@",service);
}

The NSLog message outputs Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad which is correct.

iOS Code:

NSNetService*service = [[NSNetService alloc] initWithDomain:@"local." type:@"_test._tcp." name:[[UIDevice currentDevice] name] port:28];
[service setDelegate:self];
[service setTXTRecordData:nil];
[service publish];

[service startMonitoring];


- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
{
    NSLog(@"Got Data! of %@",sender);

    NSPropertyListFormat format;
    NSDictionary*dict = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:nil];


    UIAlertView *myAlert = [[UIAlertView alloc]
                            initWithTitle:[sender name] message:[dict objectForKey:@"Version"]
                            delegate:self 
                            cancelButtonTitle:nil
                            otherButtonTitles:@"OK", nil];
    [myAlert show];  
    [myAlert release];
}

For some reason, the alert is called when I launch the iPad app (blank contents except for the title) but doesnt respond to any signals from my Mac.

I am sure I am missing something?

Only the publisher of the service can set the TXT record data. The discoverer of a published service can read the TXT record but cannot alter it. The TXT record provides a way for the publisher of the service to publish additional, publicly readable data that can be read from the DNS entry without having to contact the publisher directly.

ETA: At least, NSNetService 's documentation makes it appear like you needn't resolve the service before you can grab the TXT record data. The CFNetService documentation notes that you must resolve the service before you can get the TXT record data. Both CFNetService and NSNetService seem to work only with a subset of the possible DNS-SD records that can be created and discovered using the C API described in <dns_sd.h> , which allows for attaching multiple TXT records to a single advertized service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM