Bonjour: Publish a NSNetService in iOS/Objective-C with Service name and type

1.3k Views Asked by At

I have seen that a NSNetService can publish a Bonjour service setting up the service name and the service type. I do:

/**
 Publish this Socket Server on local domain
 @param serviceName String The service name to publish
 @param serviceType String The bonjour service name named as _serviceType._tcp
 */
- (void)publish:(NSString*)serviceName serviceType:(NSString*)serviceType
{

    self.serviceName = serviceName;
    self.serviceType = serviceType;

    self.listenSocket = [[MXMAsyncSocket alloc] initWithDelegate:self];
    NSError *error;
    if (![listenSocket acceptOnPort:0 error:&error])
    {
        [[Logger sharedLogger] error:error
                            withMessage:@"Error starting NetServer"];
        return;
    }

    int port = [listenSocket localPort];

    [[Logger sharedLogger] info:@"NetServer started [%@:%ld]", self.serverName, port];

    int seed = arc4random_uniform(9999);
    self.serverName = [NSString stringWithFormat:@"%@_%@_%d",
                       self.serviceName,
                       self.deviceName,
                       seed];

    // register itself to bonjour service.
    netService = [[NSNetService alloc] initWithDomain:@"local."
                                                  type: [NSString stringWithFormat:@"_%@._tcp", self.serviceType]
                                                  name: self.serverName
                                                  port: port];

    if (!netService)
    {
        [[Logger sharedLogger] warn:@"Failed to enable NetService [%@:%ld]", self.serverName, port];
        [listenSocket disconnect];
        return;
    }

    [netService setIncludesPeerToPeer:YES];
    [netService setDelegate:self];
    [netService publish];
    [netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

}

Now, when I look into a service browser like Bonjour Browser by TildeSoft I can see the NetService published like here:

enter image description here

I can see that I have the named services servicename under the service type myservicetype._tcp.

So how to publish a Bonjour net service having a name like AirTunes of type _airport._tcp, I mean I'm only able to publish my service by type: myservicetype._tcp.

0

There are 0 best solutions below